I had faced a weird error for a few days ago. Luckly, i had solved this error about in 4-5 day. However, i just wonder that the error why happen.
First of all, i will describe the situation with same sample of code.
@interface SampleViewController ()
@property (nonatomic, strong) NSMutableArray *selectedIssue;
@property (nonatomic, strong) NSOperationQueue *queueJSON;
@property (nonatomic, strong) NSOperationQueue *queueImage;
@end
@implementation SampleViewController
/** blah blah blah*/
- (void) fillIssueArrayMethodWithIssueId:(NSInteger) selectedId {
NSString *requestURL = [NSString stringWithFormat:@"%@Issues/Get/?id=%d", kAPIRootURL, selectedId];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestURL]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:kNetworkTimeOut];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id response) {
NSData *JSONData = [NSData dataWithContentsOfFile:filePath
options:NSDataReadingMappedIfSafe
error:nil];
NSMutableArray *responseObject = [NSJSONSerialization JSONObjectWithData:JSONData
options:NSJSONReadingMutableContainers
error:nil];
if(responseObject) {
if([responseObject isKindOfClass:[NSArray class]]) {
_selectedIssue = [NSMutableArray arrayWithArray:responseObject];
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];
[_queueJSON addOperation:op];
}
-(void)startDownloadImages {
NSArray *objPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *strPath = [objPaths lastObject];
if(!_queueImage)
_queueImage = [[NSOperationQueue alloc] init];
_queueImage.maxConcurrentOperationCount = 3;
NSBlockOperation *completionOperation = [NSBlockOperation new];
for (__block NSDictionary *object in _selectedIssue) {
// Photos
NSString *imgURL = object[@"ImageUrl"];
NSString *strImageFile = [NSString stringWithFormat:@"%@_%@", object[@"Id"], [imgURL lastPathComponent]];
__block NSString *strImagePath = [NSString stringWithFormat:@"%@/Images/Issues/%@", strPath, strImageFile];
NSURLRequest *request4Images = [NSURLRequest requestWithURL:[NSURL URLWithString:imgURL]];
AFHTTPRequestOperation *operation4Images = [[AFHTTPRequestOperation alloc] initWithRequest:request4Images];
[operation4Images setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id rawResult) {
BOOL isImageWrited = [rawResult writeToFile:strImagePath options:NSDataWritingAtomic error:nil];
if(isImageWrited) {
NSLog(@"Image Write Success : %@", operation.request.URL);
} else {
NSLog(@"Image Write Error: %@", imageWriteError.description);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Image Write Failed : %@ : %ld", operation.request.URL, (long)operation.response.statusCode);
}];
[completionOperation addDependency:operation4Images];
}
[_queueImage addOperations:completionOperation.dependencies waitUntilFinished:NO];
[_queueImage addOperation:completionOperation];
}
@end
Here is problem;
In application lifecycle, fillIssueArrayMethodWithIssueId method get JSON data to mutuablearray first. Then start to download images by getting image URL from the array.
Each second time, i want to access the _selectedIssue in fillIssueArrayMethodWithIssueId method, my app got crash (SIGSEGV)
In according to long investigation, _selectedUssue become instantly zombie object when StartDownloadImages method finished. However in this method i have never reallocated this array. I have just read values in this array. I am sure that the array become zombie object in this method so the issue fixed when removed "__block" in for loop.
So the question, __block type how could affect the _selectedIssue arrays object ?
I just want to know what i am missing...
By the way,i have been tried in StartDownloadImages method before the for loop, i had create temporary array and this temp array just initiated with _selectedIssue mutableCopy or just copy. However, i again faced with zombie problem.