A few notes generally:
Your completion block for your network request is capturing self
. This will cause retain cycles.
^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError)
{
NSLog(@"ERROR CONNECTING DATA FROM SERVER: %@", connectionError.localizedDescription);
} else {
[self.writingOperationQueue addOperationWithBlock:^{
[[NSFileManager defaultManager] createFileAtPath:[self filePath:title]
contents:data
attributes:nil];
}];
completion(data);
}
}
To fix it, you should declare a variable __weak MyClass *weakSelf = self;
before the block and only use that from within the block.
Your specific problem likely has nothing do to with the type of document you are calling and more about what thread you're calling it from. You are performing your network operation on [NSOperationQueue currentQueue]
. If this is a system queue, it is likely being deallocated before your request completes. You should declare a property that is an NSOperationQueue
and perform all your network requests on it.
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {...}];
Should be changed into:
[NSURLConnection sendAsynchronousRequest:request
queue:self.networkOpQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {...}];