(iOS)脱机同步数据库 - 服务器

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)

Gav*_*ler 1

如果这段代码放在我面前,我的方法将是:

  • 查看用例并定义“大量记录”:是否会定期更新一次 50 条记录?还是会在1秒和2秒内?我的用户有 WiFi 连接还是通过付费网络连接?等等。
  • 如果可能的话,在野外进行测试。如果我的用户群足够小,请收集真实数据并让它指导我的决策,或者仅将功能发布给用户/测试版测试和测量的子集。
  • 如果数据告诉您,请优化此代码以提高效率。

我的优化途径是进行分组处理。粗略的算法类似于:

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和不成熟的优化。

不要让这个决定阻止您发货!