Firebase可以在iOS 7的后台发送和接收吗?

前端之家收集整理的这篇文章主要介绍了Firebase可以在iOS 7的后台发送和接收吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在iOS 7上的Objective C应用程序在后台从startUpdatingsignificantLocationChanges或startUpdatingLocation委托获取位置更新(哪一个取决于应用程序所处的模式,但我认为这不重要).

在委托中,我收集位置信息,将其写入字典,然后将字典写入Firebase.

// this code is in the location update delegate routine

// the code that gathers the varIoUs elements that go into the dictionary
// are omitted for clarity,I don't think that they matter

// we may be running in the background on iOS 7 when we are called!

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
   [[NSNumber numberWithFloat:newLocation.coordinate.latitude] stringValue],@"Latitude",[[NSNumber numberWithFloat:newLocation.coordinate.longitude] stringValue],@"Longitude",[[NSNumber numberWithFloat:newLocation.horizontalAccuracy] stringValue],@"Accuracy",formattedDateString,@"TimeNow",[dateFormatter stringFromDate:newLocation.timestamp],@"TimeStamp",[[NSNumber numberWithDouble:interval] stringValue],@"Date",self.mode,@"Mode",nil];

   // Write it once to CurrentLocation
   [ref setValue:dictionary];

   // yes,I know this is clumsy
   fbTmp = [NSMutableString stringWithString: fbRoot];
   [fbTmp appendString : @"/locationHistory"];
   ref = [[Firebase alloc] initWithUrl:fbTmp]; 

   // Now write it again to the locationHistory list
   ref = [ref childByAutoId];
   [ref setValue:dictionary];

有时它可以工作,有时它不工作(即在应用程序的同一个运行中,有时位置会按预期成功写入Firebase,有时它不会.没有任何明显的押韵或理由,当它似乎工作,什么时候不工作).

我怀疑问题是Firebase写入没有在后台模式下成功完成,但我不确定.我是iOS和Objective C和Firebase的新手.

我的应用程序在其功能标记为要求位置更新和后台获取后台服务(后者我随机尝试解决此问题,前者我知道我需要).

我的怀疑是我需要告诉操作系统我需要时间来完成使用backkgroundTask的写入,然后在firebase写入的完成块中终止后台任务 – 有人确认在后台模式下运行时会有效吗?

如果是这样,我是否只需要在第二次写入(假设它们按顺序完成)或两者中都这样做(使用我在每次写入完成时倒计时的计数器)?

任何提示最受赞赏.

解决方法

是的,你需要在后台完成你的任务.它在 Apple Documentation中这样说:

If your app is in the middle of a task and needs a little extra time to complete that task,it can call the beginBackgroundTaskWithName:expirationHandler: or beginBackgroundTaskWithExpirationHandler: method of the UIApplication object to request some additional execution time. Calling either of these methods delays the suspension of your app temporarily,giving it a little extra time to finish its work. Upon completion of that work,your app must call the endBackgroundTask: method to let the system know that it is finished and can be suspended.

只需将您的代码放在后台任务中,给它最大的时间(我想3分钟).

阅读Apple app lifecycle,一切都应该清理,以备将来参考.

猜你在找的iOS相关文章