我的NSURL有一个类别来设置我的DataModel,以便以后访问我的模型对象。在这里,我的DataModel是核心数据实体(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所以,我正在通过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];
}
}每次下载成功,我都会访问我的DataModel,
- (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);
}
}
}
}除了有时候一切都很好。我要撞车了,
NSURL *destinationURL = [NSURL fileURLWithPath:filePath];说filePath是零。或者问题是我的NSURL类别的创建?
我在这里做错什么了?请提出建议。
发布于 2016-08-14 05:27:01
我知道NSURLSession不处理NSURLRequest的子类;它在iOS 7中是一场灾难,在iOS 8中是不可用的,在iOS 9中仍然很糟糕。
我认为这是因为即使在进程中处理请求时,对象也被序列化为plist。我怀疑您遇到了与我相同的bug,在这种情况下,NSURLRequest或NSURL上的相关对象可能也会有同样的问题。
作为另一种选择,下面是一些可能更适合您的其他技术:
setProperty:forKey:inRequest:和property:forKey:inRequest:。HTH。
https://stackoverflow.com/questions/38496941
复制相似问题