ios - 如何从后台的iOS应用程序在后台保存解析对象?

标签 ios parse-platform

我有一个iOS应用程序,可在后台收集位置信息并将其推送到Parse。推送时,我检查 Parse.com 的可访问性。如果可以访问,则使用saveInBackground,否则使用saveEventually。这是事件的顺序:

  • 启动应用程序
  • 按下主页按钮将其放在后台。
  • 四处移动
  • 检查Parse中的更新。

  • 我发现,当应用程序在后台运行时,更新不会被推送(在Parse浏览器中不可见)。但是,一旦我单击以将应用程序置于前台,更新就会发生,并且我会在网络上看到它们。我检查了我在plist中具有正确的权限(总是获取位置更新),并且确实看到了位置更新实际上是在我的应用程序的内存中收到的,但是它们似乎正在解析推送队列中等待离开网络。整个过程中,当应用程序处于后台或前景时,我都已连接到网络。

    在我的情况下,用户可能很长一段时间都不会单击该应用程序,但是我确实需要在Parse后端中找到用于我的后端逻辑的位置。在后台如何获取更新?我已经搜索并查看了AnyPic代码,但是它不适用于最新的iOS,因为在最新的iOS中,我们需要使用NSURL进行背景推送。

    这是我在做什么的代码段。我正在使用带有beginBackgroundtask的dispatch_async。
    var backgroundTaskId: UIBackgroundTaskIdentifier!
    if(Reachability.isConnectedToNetwork()) {
    
                // Request a background execution task to allow us to finish uploading the location even if the app is backgrounded
                self.backgroundTaskId = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler {
                    UIApplication.sharedApplication().endBackgroundTask(self.backgroundTaskId)
                }
    
                NSLog("Requested background expiration task with id \(self.backgroundTaskId) for writing:\(object)")
    
    
                // Dispatch async with network connected and then backgrounding the task currently stops
                // on-going updates which resume when the task is brought into foreground again.
                // This behaviour is of saveInBackground and not of iOS related stuff as it also happens
                // without dispatch async and background task ID. See:
                // See my question:
                //http://stackoverflow.com/questions/32427082/how-to-save-parse-objects-in-background-from-an-ios-app-that-is-in-background
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
          object.saveInBackgroundWithBlock({
            (succeeded: Bool, error: NSError?) -> Void in
    
                if(succeeded) {
    
                                UIApplication.sharedApplication().endBackgroundTask(self.backgroundTaskId)
                }
            else
                {
                    object.saveEventually()
                      UIApplication.sharedApplication().endBackgroundTask(self.backgroundTaskId)        }
    
                })    // saveInBackground
                })    // dispatch_async
            }
            else  // No Network
            {
                object.saveEventually()     
            }
    

    最佳答案

    如果要在后台推送更新,则需要启用后台模式。这是一个可以帮助您的tutorial

    关于ios - 如何从后台的iOS应用程序在后台保存解析对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32427082/

    相关文章:

    ios - 使用解析获取错误(代码 : 105, 版本 : 1. 17.2)

    ios - 为什么 Xcode 不识别 PFFacebookUtils?

    ios - 使用 Storyboard时设置默认选项卡

    ios - 解析 localDatastore 仅返回 1 个对象

    swift - 在 Swift 中使用 Cell Tags 和 objectForKey

    ios - PFQueryTableViewController...Swift

    ios - MKOverlayRenderer 比 MKOverlayView 差?

    ios - 过期的 iO​​S 开发者/分发证书

    ios - Podfile.lock没有这样的文件或目录

    ios - 是否可以从 MKPolyline 继承