Skip to content

Instantly share code, notes, and snippets.

@jonalmeida
Last active August 29, 2015 14:03
Show Gist options
  • Save jonalmeida/27bee72b9015d45434e8 to your computer and use it in GitHub Desktop.
Save jonalmeida/27bee72b9015d45434e8 to your computer and use it in GitHub Desktop.
What's the correct way to use inDatabase: within an async networking call?
- (void) sendPostData:(NSDictionary *)data withDbIndexArray:(NSMutableArray *)dbIndexArray {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:[_urlEndpoint absoluteString] parameters:data success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
[_dbQueue inDatabase:^(FMDatabase *db) {
for (int i=0; i < dbIndexArray.count; i++) {
NSLog(@"Removing event at index: %@", dbIndexArray[i]);
[_db removeEventWithId:[[dbIndexArray objectAtIndex:i] longLongValue]];
[dbIndexArray removeObjectAtIndex:i];
}
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
//Add event to queue
}];
// [dbIndexArray removeAllObjects];
}
// SO: http://stackoverflow.com/questions/24719913/whats-the-correct-way-to-use-indatabase-within-an-async-networking-call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment