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

Commit 5f9954b

Browse files
committed
Add background state support for IOS #115
1 parent e24a476 commit 5f9954b

File tree

3 files changed

+61
-76
lines changed

3 files changed

+61
-76
lines changed

src/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ const Blob = polyfill.Blob
4747
const emitter = DeviceEventEmitter
4848
const RNFetchBlob = NativeModules.RNFetchBlob
4949

50-
AppState.addEventListener('change', (e) => {
51-
console.log('app state changed', e)
52-
if(e === 'active')
53-
RNFetchBlob.emitExpiredEvent(()=>{})
54-
})
50+
// when app resumes, check if there's any expired network task and trigger
51+
// their .expire event
52+
if(Platform.OS === 'ios') {
53+
AppState.addEventListener('change', (e) => {
54+
console.log('app state changed', e)
55+
if(e === 'active')
56+
RNFetchBlob.emitExpiredEvent(()=>{})
57+
})
58+
}
5559

5660
// register message channel event handler.
5761
emitter.addListener("RNFetchBlobMessage", (e) => {
@@ -132,7 +136,7 @@ function fetchFile(options = {}, method, url, headers = {}, body):Promise {
132136
let total = -1
133137
let cacheData = ''
134138
let info = null
135-
139+
let {x,y} = a.props
136140
let _progress, _uploadProgress, _stateChange
137141

138142
switch(method.toLowerCase()) {

src/ios/RNFetchBlobNetwork.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,10 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
264264
__block UIApplication * app = [UIApplication sharedApplication];
265265

266266
// #115 handling task expired when application entering backgound for a long time
267-
[app beginBackgroundTaskWithName:taskId expirationHandler:^{
267+
UIBackgroundTaskIdentifier tid = [app beginBackgroundTaskWithName:taskId expirationHandler:^{
268268
NSLog([NSString stringWithFormat:@"session %@ expired", taskId ]);
269269
[expirationTable setObject:task forKey:taskId];
270-
[app endBackgroundTask:task];
271-
270+
[app endBackgroundTask:tid];
272271
}];
273272

274273

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

290-
// emit expired event once
290+
// clear expired task entries
291291
[expirationTable removeAllObjects];
292+
expirationTable = [[NSMapTable alloc] init];
293+
292294
}
293295

294296
////////////////////////////////////////

test/test-0.10.0.js

Lines changed: 45 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -27,51 +27,51 @@ const dirs = RNFetchBlob.fs.dirs
2727
let prefix = ((Platform.OS === 'android') ? 'file://' : '')
2828
let begin = Date.now()
2929

30-
// describe('json stream via HTTP', (report, done) => {
31-
//
32-
// let count = 0
33-
// JSONStream(`${TEST_SERVER_URL}/public/json-dummy.json`).node('name', (name) => {
34-
// count++
35-
// if(Date.now() - begin < 100)
36-
// return
37-
// begin = Date.now()
38-
// report(<Info key="report" uid="100">
39-
// <Text>{count} records</Text>
40-
// </Info>)
41-
// done()
42-
// })
43-
//
44-
// })
45-
//
46-
// describe('json stream via fs', (report, done) => {
47-
//
48-
// let fetch2 = new RNFetchBlob.polyfill.Fetch({
49-
// auto : true
50-
// })
51-
// let res = null
52-
// let count = 0
53-
//
54-
// RNFetchBlob.config({
55-
// fileCache : true
56-
// })
57-
// .fetch('GET',`${TEST_SERVER_URL}/public/json-dummy.json`)
58-
// .then((resp) => {
59-
// res = resp
60-
// JSONStream({
61-
// url : RNFetchBlob.wrap(res.path()),
62-
// headers : { bufferSize : 10240 }
63-
// }).node('name', (name) => {
64-
// count++
65-
// if(Date.now() - begin < 100)
66-
// return
67-
// begin = Date.now()
68-
// report(<Info key="report" uid="100">
69-
// <Text>{count} records</Text>
70-
// </Info>)
71-
// done()
72-
// })
73-
// })
74-
// })
30+
describe('json stream via HTTP', (report, done) => {
31+
32+
let count = 0
33+
JSONStream(`${TEST_SERVER_URL}/public/json-dummy.json`).node('name', (name) => {
34+
count++
35+
if(Date.now() - begin < 100)
36+
return
37+
begin = Date.now()
38+
report(<Info key="report" uid="100">
39+
<Text>{count} records</Text>
40+
</Info>)
41+
done()
42+
})
43+
44+
})
45+
46+
describe('json stream via fs', (report, done) => {
47+
48+
let fetch2 = new RNFetchBlob.polyfill.Fetch({
49+
auto : true
50+
})
51+
let res = null
52+
let count = 0
53+
54+
RNFetchBlob.config({
55+
fileCache : true
56+
})
57+
.fetch('GET',`${TEST_SERVER_URL}/public/json-dummy.json`)
58+
.then((resp) => {
59+
res = resp
60+
JSONStream({
61+
url : RNFetchBlob.wrap(res.path()),
62+
headers : { bufferSize : 10240 }
63+
}).node('name', (name) => {
64+
count++
65+
if(Date.now() - begin < 100)
66+
return
67+
begin = Date.now()
68+
report(<Info key="report" uid="100">
69+
<Text>{count} records</Text>
70+
</Info>)
71+
done()
72+
})
73+
})
74+
})
7575
//
7676
// describe('issue #102', (report, done) => {
7777
// let tmp = null
@@ -142,24 +142,3 @@ let begin = Date.now()
142142
// })
143143
//
144144
// })
145-
146-
147-
describe('action view intent get callback', (report, done) => {
148-
let isActionViewVisible = false
149-
150-
RNFetchBlob.config({
151-
fileCache : true,
152-
appendExt : 'png',
153-
trusty : true
154-
})
155-
.fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
156-
.then(res => {
157-
console.log(res.data);
158-
console.log(res.info())
159-
RNFetchBlob.android.actionViewIntent(res.path(), 'image/png')
160-
.then(() => {
161-
console.log('action-view removed')
162-
isActionViewVisible = true
163-
});
164-
})
165-
})

0 commit comments

Comments
 (0)