タイマーのアイデアは機能します。あなたのアプリケーションに適したある間隔でタイマーを介してデータマネージャークラスのuploadOutstandingObjectsを呼び出してください。
アップロードする必要のある「Foo」エンティティがあるとします。データマネージャークラスで次の操作を実行できます。
- (void)uploadOutstandingObjects {
// I use the great MagicalRecord class for Core Data fetching
// https://github.com/magicalpanda/MagicalRecord
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status == pending"]
NSArray *outstandingObjects = [Foo MR_findAllWithPredicate:predicate];
for (Foo *foo in outstandingObjects) {
[foo uploadToServer];
}
これを行う方法の1つは、通知を使用することです。アップロードを開始するたびに、そのオブジェクトは「uploadsStopped」通知を聞きます。アップロードが完了すると、アップロードされているオブジェクトはリスニングを停止します。
のFooクラス:
- (void)uploadFailed {
// change status to upload pending in the database for this 'foo' object
}
- (void)uploadComplete {
// change status to upload complete in the database for this 'foo' object
}
-(void)uploadToServer {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(uploadFailed:)
name:@"uploadsStoppedNotification"
object:nil ];
// perform upload. If you are doing this synchronously...
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:<url here>];
[request startSynchronously];
if (![request error]) {
[self uploadSucceeded];
// stop listening to global upload notifications as upload attempt is over
[NSNotificationCenter removeObserver:self];
}
else {
[self uploadFailed];
// stop listening to global upload notifications as upload attempt is over
[NSNotificationCenter removeObserver:self];
}
あなたのアプリは、あなたがまだ来て人々のための代替手段として
- (void)applicationDidEnterBackground:(UIApplication *)application {
// this will fire to any objects which are listening to
// the "uploadsStoppedNotification"
[[NSNotificationCenter defaultCenter]
postNotificationName:@"uploadsStoppedNotification"
object:nil ];