Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Commit

Permalink
Add background state support for IOS #115
Browse files Browse the repository at this point in the history
  • Loading branch information
wkh237 committed Nov 2, 2016
1 parent e24a476 commit 5f9954b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 76 deletions.
16 changes: 10 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ const Blob = polyfill.Blob
const emitter = DeviceEventEmitter
const RNFetchBlob = NativeModules.RNFetchBlob

AppState.addEventListener('change', (e) => {
console.log('app state changed', e)
if(e === 'active')
RNFetchBlob.emitExpiredEvent(()=>{})
})
// when app resumes, check if there's any expired network task and trigger
// their .expire event
if(Platform.OS === 'ios') {
AppState.addEventListener('change', (e) => {
console.log('app state changed', e)
if(e === 'active')
RNFetchBlob.emitExpiredEvent(()=>{})
})
}

// register message channel event handler.
emitter.addListener("RNFetchBlobMessage", (e) => {
Expand Down Expand Up @@ -132,7 +136,7 @@ function fetchFile(options = {}, method, url, headers = {}, body):Promise {
let total = -1
let cacheData = ''
let info = null

let {x,y} = a.props

This comment has been minimized.

Copy link
@francisco-sanchez-molina

francisco-sanchez-molina Nov 2, 2016

Contributor

is this an error?

Thank you for your awesome project!!

This comment has been minimized.

Copy link
@wkh237

wkh237 Nov 3, 2016

Author Owner

Yeah .. this is definitely a mistake, thanks for notice !

let _progress, _uploadProgress, _stateChange

switch(method.toLowerCase()) {
Expand Down
10 changes: 6 additions & 4 deletions src/ios/RNFetchBlobNetwork.m
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,10 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
__block UIApplication * app = [UIApplication sharedApplication];

// #115 handling task expired when application entering backgound for a long time
[app beginBackgroundTaskWithName:taskId expirationHandler:^{
UIBackgroundTaskIdentifier tid = [app beginBackgroundTaskWithName:taskId expirationHandler:^{
NSLog([NSString stringWithFormat:@"session %@ expired", taskId ]);
[expirationTable setObject:task forKey:taskId];
[app endBackgroundTask:task];

[app endBackgroundTask:tid];
}];


Expand All @@ -285,10 +284,13 @@ + (void) emitExpiredTasks
RCTBridge * bridge = [RNFetchBlob getRCTBridge];
NSData * args = @{ @"taskId": key };
[bridge.eventDispatcher sendDeviceEventWithName:EVENT_EXPIRE body:args];

}

// emit expired event once
// clear expired task entries
[expirationTable removeAllObjects];
expirationTable = [[NSMapTable alloc] init];

}

////////////////////////////////////////
Expand Down
111 changes: 45 additions & 66 deletions test/test-0.10.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,51 @@ const dirs = RNFetchBlob.fs.dirs
let prefix = ((Platform.OS === 'android') ? 'file://' : '')
let begin = Date.now()

// describe('json stream via HTTP', (report, done) => {
//
// let count = 0
// JSONStream(`${TEST_SERVER_URL}/public/json-dummy.json`).node('name', (name) => {
// count++
// if(Date.now() - begin < 100)
// return
// begin = Date.now()
// report(<Info key="report" uid="100">
// <Text>{count} records</Text>
// </Info>)
// done()
// })
//
// })
//
// describe('json stream via fs', (report, done) => {
//
// let fetch2 = new RNFetchBlob.polyfill.Fetch({
// auto : true
// })
// let res = null
// let count = 0
//
// RNFetchBlob.config({
// fileCache : true
// })
// .fetch('GET',`${TEST_SERVER_URL}/public/json-dummy.json`)
// .then((resp) => {
// res = resp
// JSONStream({
// url : RNFetchBlob.wrap(res.path()),
// headers : { bufferSize : 10240 }
// }).node('name', (name) => {
// count++
// if(Date.now() - begin < 100)
// return
// begin = Date.now()
// report(<Info key="report" uid="100">
// <Text>{count} records</Text>
// </Info>)
// done()
// })
// })
// })
describe('json stream via HTTP', (report, done) => {

let count = 0
JSONStream(`${TEST_SERVER_URL}/public/json-dummy.json`).node('name', (name) => {
count++
if(Date.now() - begin < 100)
return
begin = Date.now()
report(<Info key="report" uid="100">
<Text>{count} records</Text>
</Info>)
done()
})

})

describe('json stream via fs', (report, done) => {

let fetch2 = new RNFetchBlob.polyfill.Fetch({
auto : true
})
let res = null
let count = 0

RNFetchBlob.config({
fileCache : true
})
.fetch('GET',`${TEST_SERVER_URL}/public/json-dummy.json`)
.then((resp) => {
res = resp
JSONStream({
url : RNFetchBlob.wrap(res.path()),
headers : { bufferSize : 10240 }
}).node('name', (name) => {
count++
if(Date.now() - begin < 100)
return
begin = Date.now()
report(<Info key="report" uid="100">
<Text>{count} records</Text>
</Info>)
done()
})
})
})
//
// describe('issue #102', (report, done) => {
// let tmp = null
Expand Down Expand Up @@ -142,24 +142,3 @@ let begin = Date.now()
// })
//
// })


describe('action view intent get callback', (report, done) => {
let isActionViewVisible = false

RNFetchBlob.config({
fileCache : true,
appendExt : 'png',
trusty : true
})
.fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
.then(res => {
console.log(res.data);
console.log(res.info())
RNFetchBlob.android.actionViewIntent(res.path(), 'image/png')
.then(() => {
console.log('action-view removed')
isActionViewVisible = true
});
})
})

0 comments on commit 5f9954b

Please sign in to comment.