2014-11-04 2 views
0

추천을위한 iphone 앱을 개발 중입니다. Parse를 사용하여 사용자, 연락처 및 추천을 유지 관리합니다. 또한 핵심 데이터를 사용하여 구문 분석과 동기화하고 오프라인 작업을 유지 관리합니다. Parse와 Core Data를 동기화하는 새로 고침 메소드가 있습니다. 사용자가 앱을 종료하자마자 Parse와의 동기화가 백그라운드에서 수행되는 방법을 찾으려고합니다. applicationDidEnterBackground에서 간단한 구문 분석 쿼리를 사용하여 테스트했지만 작동하지 않습니다. 다음은 내가 사용한 코드입니다.구문 분석을 사용하여 백그라운드 프로세스를 구현하는 방법

(void)applicationDidEnterBackground:(UIApplication *)application{ 

    self.backgroundTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{ 
     [application endBackgroundTask:self.backgroundTask]; 
     self.backgroundTask = UIBackgroundTaskInvalid; 
    }]; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     NSString *message = @"begin background task"; 
     NSDictionary *dict = @{@"status": message}; 
     [[NSUserDefaults standardUserDefaults] setObject:dict forKey:kBeginBackgroundTaskWithNameKey]; 

     PFQuery *query = [PFQuery queryWithClassName:@"Referral"]; 

     [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
      if (!error) { 
       NSString *message = [NSString stringWithFormat:@"array has %lu objects", (unsigned long)objects.count]; 
       NSDictionary *dict = @{@"status": message}; 
       [[NSUserDefaults standardUserDefaults] setObject:dict forKey:kBeginBackgroundTaskWithNameKey]; 

       [application endBackgroundTask:self.backgroundTask]; 
       self.backgroundTask = UIBackgroundTaskInvalid; 
      } 

     }];  
    }); 
} 

내가 뭘 잘못하고 있니?

답변

0

마침내 내 문제를 해결하는 방법을 찾았습니다. 나는 지침서 http://www.raywenderlich.com/29948/backgrounding-for-ios을 위해 Ray Wenderlich 튜토리얼 "iOS의 배경 모드"를 사용했다. AppDelegate에 여기 주요 변화 :

- (무효) parseSync {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    // Parse code .... 

}); 


self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
    NSLog(@"Background handler called. Not running background tasks anymore."); 
    [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask]; 
    self.backgroundTask = UIBackgroundTaskInvalid; 
}]; 

}

  • (무효) applicationWillResignActive (UIApplication *) 애플리케이션 {

    [자기 parseSync ];

}

관련 문제