我有一個應用程序從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。
我想你是對的。我能夠建立一個配置爲在後臺運行的FURLSessionManager,但我努力使我的代碼符合上述新代碼。我已經搜索了所有內容,但找不到一個例子,我相信這是一個例子。我將在上面發佈我的代碼。 –