Skip to content

Instantly share code, notes, and snippets.

@hramos
Created September 27, 2011 03:08
Show Gist options
  • Save hramos/1244223 to your computer and use it in GitHub Desktop.
Save hramos/1244223 to your computer and use it in GitHub Desktop.
Parse SDK 0.2.15 is blocking main thread when preparing to upload a PFObject with lots of data. Moving this to a background thread avoids UI freeze
// original file: PFRequest.m
- (void)buildAndSignPostBody {
for (NSString *key in self.command.params) {
[self setPostValue:[self.command.params objectForKey:key] forKey:key];
}
// This also ensures that posts are not empty, which would break various things
[self setPostValue:[NSString stringWithFormat:@"i%@", PARSE_VERSION] forKey:@"v"];
__block NSString *header = nil;
/* I'm using DISPATCH_QUEUE_PRIORITY_LOW, as DISPATCH_QUEUE_PRIORITY_BACKGROUND
* is not available until 4.3
*/
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Background Thread
[self buildPostBody];
header = PF_OAuthorizationHeader([self url],
[self requestMethod],
[self postBody],
APPLICATION_ID,
CLIENT_KEY,
nil,
nil);
dispatch_sync(dispatch_get_main_queue(), ^{
// Main Thread
[self addRequestHeader:@"Authorization" value:header];
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment