尝试实现一个应用程序,当连接到互联网时将存储在本地数据库中的离线数据发送到Web服务器。我使用下面显示的代码。据我测试,它运行良好,但不确定对于大量记录是否有效。我想知道是否有任何调整此代码可以提高性能?
注意
- I know this would be a worst code for offline sync purpose, so trying to tweak it better.
Its a single way synchronization, from app to server.
-(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]; }); }