I have a category for my NSURL
to set my DataModel
for accessing my model objects later. Here, my DataModel
is subclassed of my Core Data entity (NSManagedObject)
@implementation NSURL (CustomizedObject)
static char PROPERTY_KEY;
@dynamic model;
- (void)setChunk:(DataModel *)model {
objc_setAssociatedObject(self, &PROPERTY_KEY, model, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (DataModel *)model {
return (DataModel *)objc_getAssociatedObject(self, &PROPERTY_KEY);
}
@end
So, i'm downloading my data through NSURLSessionDownloadTask
for (DataModel *model in models) { // Here 985 URLs are there
if ([model.status integerValue] == 0) {
NSURL *requestURL = [NSURL URLWithString:model.downloadSequence];
[requestURL setModel:model];
NSURLSessionDownloadTask *downloadTask = [self.session downloadTaskWithURL:requestURL];
[downloadTask resume];
[self.arrFileDownloadData addObject:downloadTask];
}
}
And, i'm accessing my DataModel
in every successful download,
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
self.downloadSuccessCount++;
DownloadModel *model = downloadTask.originalRequest.URL.model;
NSString *filePath = [model.content.filePath stringByAppendingPathComponent:[[model.streamingSequence lastPathComponent] stringByDeletingPathExtension]];
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *destinationURL = [NSURL fileURLWithPath:filePath];
if ([fileManager fileExistsAtPath:[destinationURL path]]) {
[fileManager removeItemAtURL:destinationURL error:nil];
}
BOOL success = [fileManager copyItemAtURL:location toURL:destinationURL error:&error];
if (success) {
NSError *fetchError = nil;
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:ChunkCoreData];
[fetch setPredicate:[NSPredicate predicateWithFormat: @"modelId == %@", model.modelId]];
NSArray *isProductExist = [self.context executeFetchRequest:fetch error:&fetchError];
if ([isProductExist count] != 0) {
DataModel *fetchedModel = [isProductExist lastObject];
fetchedModel.status = [NSNumber numberWithInt:2];
// create writer MOC
NSManagedObjectContext* _privateWriterContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_privateWriterContext setPersistentStoreCoordinator:[[self managedObjectContext] persistentStoreCoordinator]];
// create main thread MOC
NSManagedObjectContext* _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_managedObjectContext.parentContext = _privateWriterContext;
NSError *saveError = nil;
if (![_privateWriterContext save:&saveError]) {
NSLog(@"Couldn't save %@", saveError.localizedDescription);
}
}
}
}
Everything, works fine except sometimes. I'm getting crash in,
NSURL *destinationURL = [NSURL fileURLWithPath:filePath];
to says that filePath
is nil. Or the issue is with my NSURL
category creation?
What i'm doing here wrong? Suggestions please.