【问题标题】:saveEventually - Save when application is minimised with ParsesaveEventually - 使用 Parse 最小化应用程序时保存
【发布时间】:2014-08-26 13:00:39
【问题描述】:

我在下面的代码部分使用saveEventually。问题是,当应用程序被最小化并重新打开时,saveEventually 不起作用,只有当应用程序完全关闭并重新打开时。有什么办法可以解决吗?

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
        // Generate Random Number
        int random = arc4random_uniform(1000000000);

        // Updates Location
        NSLog(@"Location: %@",newLocation);
        CLLocation *curentLocation = newLocation;

            if (curentLocation != nil) {

            // Submit through Parse
            PFObject *object = [PFObject objectWithClassName:@"Issue"];
            object[@"ID"] = [NSString stringWithFormat:@"%i",random];
            object[@"Latitude"] = [NSString stringWithFormat:@"%.8f",curentLocation.coordinate.latitude];
            object[@"Longitude"] = [NSString stringWithFormat:@"%.8f",curentLocation.coordinate.longitude];
            object[@"Signal"] = [NSString stringWithFormat:@"%@",_avgNumber];
            [object saveEventually];

            // Update Text Fields
            self.latitude.text = [NSString stringWithFormat:@"%.8f",curentLocation.coordinate.latitude];
            self.longitude.text = [NSString stringWithFormat:@"%.8f",curentLocation.coordinate.longitude];
            self.signal.text = [NSString stringWithFormat:@"%@",_avgNumber];

            }

    // Stop the Location Update
    [manager stopUpdatingLocation];

}

【问题讨论】:

  • 您没有使用 saveInBackground 有什么原因吗?
  • 是的,由于它捕获的信息的性质,我的应用程序很可能会离线使用,因此 saveEventually 应该可以防止数据在离线时丢失。

标签: ios objective-c parse-platform


【解决方案1】:

saveEventually 将在您描述的两种情况下工作。来自 Parse:“使用此方法保存的对象将本地存储在磁盘缓存中,直到它们可以交付给 Parse。 如果可能,它们将立即发送。否则,它们将在下次网络连接可用时发送。"

它应该可以工作,但如果不是,作为临时解决方法,您可以在保存对象之前检查连接性。如果您有互联网连接,请使用saveInBackground,如果没有,请使用saveEventually。所以是这样的:

if (isNetworkAvailable)
    [object saveInBackground];
else
    [object saveEventually];

- (BOOL)isNetworkAvailable
{
   CFNetDiagnosticRef dReference;        
   dReference = CFNetDiagnosticCreateWithURL (NULL, (__bridge CFURLRef)[NSURL URLWithString:@"www.apple.com"]);

   CFNetDiagnosticStatus status;
   status = CFNetDiagnosticCopyNetworkStatusPassively (dReference, NULL);        

   CFRelease (dReference);

   if ( status == kCFNetDiagnosticConnectionUp )
   {
      NSLog (@"Connection is Available");
      return YES;
   }
   else 
   {
    NSLog (@"Connection is down");
    return NO;
   }
}

【讨论】:

  • 理论上你是正确的,但实际上该方法并没有执行。如前所述,问题是在应用程序最小化并重新打开后重新建立连接时不会发送对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多