I have an application that loads some data from am web service. Everything works pretty well.
Now I want to update the data in a background process. Wrote that part and I can get it to kick off by using the "Simulate Background Fetch" debug process in Xcode.
My issue is that the block in the AFHTTPRequestOperation
is not getting executed. I bet I don't want a block here, but
a) I want to know why it won't execute - my guess is that you can't spin off blocks like this in background fetches.
b) I don't know how to NOT use a block here.
Any help would be greatly appreciated.
- (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];
}
======================================
The poster below is correct. I have added this code:
NSURLSessionConfiguration *backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"background"];
AFURLSessionManager *backgroundManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:backgroundConfiguration];
However, I do not understand how to add my JSON URL request to the AFURLSessionManager.
I don't know the internals of AFNetworking , but background fetch needs to be done with NSURLSession and NSURLSessionDownloadTask objects. Check if the functions you are calling are using the appropriate classes internally - methods that work fine in normal operation may simply refuse to work in background fetch mode.
After brief look at AFNetworking docs, I think you will probably need to set up an instance of AFURLSessionManager configured for background fetching.
As Peter answered, you need to use NSURLSessions. AFNetworking does provide a session manager. Check it out here .
For background tasks you need to create a NSURLSessionConfiguration with a background configuration. Link to Apple Docs
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration backgroundSessionConfiguration:@"com.yourapp.background.download"]];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.