Nin*_*ina 8 sqlite synchronization objective-c ios
尝试实现一个应用程序,在连接到Internet时将存储在本地数据库中的脱机数据发送到Web服务器.我使用下面显示的代码.到目前为止,我测试它工作正常,不确定它将适用于大量的记录.我想知道是否对此代码进行任何调整可能会提高性能???
注意
从app到服务器的单向同步.
-(void)FormatAnswersInJSON {
DMInternetReachability *checkInternet = [[DMInternetReachability alloc] init];
if ([checkInternet isInternetReachable]) {
if ([checkInternet isHostReachable:@"www.apple.com"]) {//Change to domain
responseArray = [[NSMutableArray alloc] init];
dispatch_async(backgroundQueue, ^(void) {
NSArray *auditIDArray = [[NSArray alloc] initWithArray: [self getUnuploadedIDs]];
for (int temp = 0; temp < [auditIDArray count]; temp ++) {
// Code to post JSON to server
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
NSString *responseID = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
if ([responseID isEqualToString:@"ERROR"]) {
//Error uploading records
} else {
[responseArray addObject:responseID];
}
} else {
//Error
return;
}
}
dispatch_async( backgroundQueue, ^{
/* Based on return code update local DB */
for (int temp = 0; temp < [responseArray count]; temp ++) {
[self updateRecordsForID:[auditIDArray objectAtIndex:temp] withID:[responseArray objectAtIndex:temp]];
}
});
});
}
}
}
- (void)upload { //Called when internet connection available
if(backgroundQueue){
dispatch_suspend(backgroundQueue);
dispatch_release(backgroundQueue);
backgroundQueue = nil;
}
backgroundQueue = dispatch_queue_create("com.XXXX.TestApp.bgqueue", NULL);
dispatch_async(backgroundQueue, ^(void) {
[self FormatAnswersInJSON];
});
}
Run Code Online (Sandbox Code Playgroud)如果这段代码放在我面前,我的方法将是:
我的优化途径是进行分组处理。粗略的算法类似于:
for records in groups of X
collect
post to server {
on return:
gather records that updated successfully
update locally
}
Run Code Online (Sandbox Code Playgroud)
这假设您可以修改服务器代码。您可以进行 10、20、50 等一组。这一切都取决于发送的数据类型和大小。
组算法意味着客户端需要进行更多的预处理,但具有减少 HTTP 请求的优点。如果您只想获得少量更新,那么这就是YAGNI和不成熟的优化。
不要让这个决定阻止您发货!
| 归档时间: |
|
| 查看次数: |
1587 次 |
| 最近记录: |