2014-01-09 80 views
0

我有一个应用程序从web服务加载一些数据。一切工作都很好。iOS后台进程和AFNetworking

现在我想更新后台进程中的数据。写了这部分,我可以通过在Xcode中使用“模拟背景获取”调试过程来启动它。
我的问题是AFHTTPRequestOperation中的块没有执行。我敢打赌,我不想在这里建立一个街区,但是
a)我想知道为什么它不会执行 - 我的猜测是,你不能在后台获取这样的街区。 b)我不知道如何不在这里使用块。

任何帮助将不胜感激。

- (void) callWebServiceUpdate { 

    //Delete tmpDatabase if it is there 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    BOOL success; 
    success = [fileManager fileExistsAtPath:self.tmpEmployeeDatabasePath]; 
    if (success) { 
     [fileManager removeItemAtPath:self.tmpEmployeeDatabasePath error:nil]; 
    } 

    //Write data to temp database 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    manager.requestSerializer = [AFJSONRequestSerializer serializer]; 
    [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"USER" password:@"PASSWORD"]; 
    manager.responseSerializer = [AFJSONResponseSerializer serializer]; 

    AFHTTPRequestOperation *operationNow = [manager GET: @"https://xxxxxxxxx/mobile/mobilede.nsf/restServices.xsp/LotusDirectoryPeople" 
              parameters: [self jsonDict] 
              success: ^(AFHTTPRequestOperation *operation, id responseObject) 

             { 
              NSMutableArray *employees = (NSMutableArray *)responseObject; 
              FMDatabase *db = [FMDatabase databaseWithPath:self.tmpEmployeeDatabaseName]; 
              [db open]; 
              for(NSDictionary *dict in employees) 
              { 
               BOOL success = [db executeUpdate:@"INSERT INTO tmpemployees (id,firstName,lastName,fullName,email) VALUES (?,?,?,?,?);", 
                    [dict objectForKey:@"clockNumber"], 
                    [dict objectForKey:@"firstName"], 
                    [dict objectForKey:@"lastName"], 
                    [dict objectForKey:@"fullName"], 
                    [dict objectForKey:@"email"], 
                    nil]; 
               if (success) {} // Only to remove success error 
              } 
             } 

              failure:^(AFHTTPRequestOperation *operation, NSError *error) 
             {NSLog(@"Error: %@", error);} 
             ]; 
    [operationNow start]; 

    //Loop through the tmp db and see if we have a value to update or add 
    //id tmpDatabasePath = [(AppDelegate *)[[UIApplication sharedApplication] delegate] databasePath]; 
    NSMutableArray *tmpEmployees; 
    tmpEmployees = [[NSMutableArray alloc] init]; 

    FMDatabase *tmpDatabase = [FMDatabase databaseWithPath:self.tmpEmployeeDatabasePath]; 
    [tmpDatabase open]; 

    //id realDatabasePath = [(AppDelegate *)[[UIApplication sharedApplication] delegate] databasePath]; 
    FMDatabase *realDatabase = [FMDatabase databaseWithPath:self.employeeDatabasePath]; 
    [realDatabase open]; 


    FMResultSet *results = [tmpDatabase executeQuery:@"SELECT * FROM employees"]; 
    while([results next]) 
    { 
     employee *thisEmployee  = [employee new]; 
     thisEmployee.firstName  = [results stringForColumn:@"firstName"]; 
     thisEmployee.lastName  = [results stringForColumn:@"lastName"]; 
     thisEmployee.fullName  = [results stringForColumn:@"fullname"]; 
     thisEmployee.city   = [results stringForColumn:@"city"]; 
     thisEmployee.ste   = [results stringForColumn:@"state"]; 
     thisEmployee.emailAddress = [results stringForColumn:@"email"]; 

     NSString *tst = [results stringForColumn:@"id"]; 
     thisEmployee.id = [NSNumber numberWithInt:[tst intValue]]; 

     //See if we have a record 
     FMResultSet *results = [realDatabase executeQuery:@"SELECT * from db where id= ?",thisEmployee.id]; 




    } 
    [tmpDatabase close]; 
} 

======================================

的下面的海报是正确的。我加入这个代码:

NSURLSessionConfiguration *backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"background"]; 
AFURLSessionManager *backgroundManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:backgroundConfiguration]; 

不过,我不明白怎么我的JSON URL请求添加到AFURLSessionManager。

回答

2

我不知道AFNetworking的内部,而是一个后台获取需要与NSURLSessionNSURLSessionDownloadTask对象完成。检查你调用的函数是否在内部使用适当的类 - 在正常操作中正常工作的方法可能会拒绝在后台获取模式下工作。

介绍一下AFNetworking文档后,我想你可能会需要设置配置为背景获取AFURLSessionManager的一个实例。

+1

我想你是对的。我能够建立一个配置为在后台运行的FURLSessionManager,但我努力使我的代码符合上述新代码。我已经搜索了所有内容,但找不到一个例子,我相信这是一个例子。我将在上面发布我的代码。 –

1

正如彼得所说,你需要使用NSURLSessions。 AFNetworking确实提供会话管理器。检查出here

对于后台任务,您需要使用后台配置创建NSURLSessionConfiguration。 Link to Apple Docs

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration backgroundSessionConfiguration:@"com.yourapp.background.download"]];