我有一个从web服务加载一些数据的应用程序。一切都很好。
现在我想在后台进程中更新数据。写了这个部分,我可以通过在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。
答案 0 :(得分:2)
我不知道 AFNetworking 的内部结构,但需要使用 NSURLSession 和 NSURLSessionDownloadTask 对象完成后台提取。检查您调用的函数是否在内部使用适当的类 - 在正常操作中正常工作的方法可能只是拒绝在后台获取模式下工作。
在简要了解 AFNetworking 文档之后,我想您可能需要设置一个为背景提取配置的 AFURLSessionManager 实例。
答案 1 :(得分:1)
彼得回答说,你需要使用NSURLSessions。 AFNetworking确实提供了会话管理器。请查看here。
对于后台任务,您需要使用后台配置创建NSURLSessionConfiguration。 Link to Apple Docs
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration backgroundSessionConfiguration:@"com.yourapp.background.download"]];