2015-01-09 37 views
0

使用後臺獲取我們觸發方法分析查詢未返回時,iOS應用程序是在後臺,由後臺觸發了取

- (void)sendPush:(SPUserInfo *)userInfo { 

    PFQuery *findUserQuery = [PFUser query]; 
    [findUserQuery whereKey:@"objectId" equalTo:userInfo.userID]; 
    [findUserQuery findObjectsInBackgroundWithBlock:^(NSArray* arr, NSError* err) { 
     if (err) { 
      MLog(@"Error Finding User to Spark: %@", err.description); 
     } 
     else { 
      if (arr.count) { 
       MLog(@"User found"); 
       PFObject *spark = [PFObject objectWithClassName:kSparkClassName]; 

       [...] 

       [spark saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
        if (!error) { 
         //save 
         // send push through Parse 

         [...] 
         PFPush *push = [PFPush push]; 
         NSString *channel = [NSString stringWithFormat:@"u_%@", receiver.objectId]; 
         [push setChannels:@[channel]]; 
         [push setData:data]; 
         [push sendPushInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
          if (nil == error) { 
           MLog(@"Push success"); 
           //do the stuff 
           [self.queueSendingSpark removeObject:userInfo]; 
           [self saveSendingSparkQueue]; 
          } 
          else { 
           MLog(@"Push Error : %@", error.description); 
          } 
         }]; 

        } 
        else { 
         MLog(@"Spark is not saved on parse | %@", error); 
        }}]; 
      } 
      else { 
       MLog(@"ZERO Users"); 
      } 
     } 
    }]; 
} 

如果應用程序在前臺或如果正在運行的這是所有罰款和花花公子電話醒了。 但是當iPhone被鎖定/睡着時,findObjectsInBackgroundWithBlock從不返回任何數據。

這是預期的行爲嗎?有沒有辦法確保這個查詢和其他人在設備睡着時正常返回?

回答

1

所以那裏有,你可以用它來處理這個屬性...

@property (nonatomic, assign) UIBackgroundTaskIdentifier fileUploadBackgroundTaskId; 

在你的init方法設置屬性,像這樣:在需要的時候

self.fileUploadBackgroundTaskId = UIBackgroundTaskInvalid; 

設定值

// Request a background execution task to allow us to finish uploading the photo even if the app is backgrounded 
self.fileUploadBackgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
    [[UIApplication sharedApplication] endBackgroundTask:self.fileUploadBackgroundTaskId]; 
}]; 

運行您的查詢,然後當查詢完成時...

[[UIApplication sharedApplication] endBackgroundTask:self.fileUploadBackgroundTaskId]; 
+0

這是關於如何處理應用程序轉到後臺的Anypic解決方案。我還沒有測試過,但 – jsetting32

+0

非常感謝!似乎很好地工作。 –

+0

甜!很高興我能幫上忙! – jsetting32

相關問題