And*_*rew 15 multithreading objective-c ios ios7 nsurlsession
所以我在主线程上创建我的下载
NSURLRequest *request = [NSURLRequest requestWithURL:download.URL];
NSURLSessionDownloadTask *downloadTask = [self.downloadSession downloadTaskWithRequest:request];
[downloadTask resume];
Run Code Online (Sandbox Code Playgroud)
并将与下载相关联的NSManagedContextID添加到NSMutableDictionary中,以便稍后在委托回调中检索它
[self.downloads setObject:[download objectID] forKey:[NSNumber numberWithInteger:downloadTask.taskIdentifier]];
Run Code Online (Sandbox Code Playgroud)
我的self.downloadSession上面配置是这样的
- (NSURLSession *)backgroundSession
{
static NSURLSession *session = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.test.backgroundSession"];
configuration.discretionary = YES;
session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
});
return session;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是委托回调似乎在不同的线程上调用
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
NSManagedObjectID *downloadID = [self.downloads objectForKey:[NSNumber numberWithInteger:downloadTask.taskIdentifier]];
double progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:downloadID,@"download",[NSNumber numberWithDouble:progress],@"progress", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadProgress" object:nil userInfo:userInfo];
}
Run Code Online (Sandbox Code Playgroud)
因此,当我访问self.downloads以获取正确的objectID时,我实际上是从与其创建的不同的线程访问NSMutableDictionary,并且我相信NSMutableDictionary不是线程安全的.那么什么是最好的解决方案,我可以使用这样的东西
session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
Run Code Online (Sandbox Code Playgroud)
在声明会话时,将委托队列设置为mainQueue,这会导致在主线程上调用所有委托,但是如果可能的话,我想将所有的回调保留在后台线程上
sof*_*der 10
在您的示例中,这不是问题,因为您的字典被移交给通知系统,并且操作队列线程不再使用它.当可能同时从多个线程访问对象时,线程安全性只是一个问题.
如果你的dict是iVar你应该这样做:
像这样创建自己的队列
myQueue = [[NSOperationQueue alloc] init];
// This creates basically a serial queue, since there is just on operation running at any time.
[myQueue setMaxConcurrentOperationCount:1];
Run Code Online (Sandbox Code Playgroud)
然后在此队列上安排对您的字典的每次访问,例如:
[myQueue addOperationWithBlock:^
{
// Access your dictionary
}];
Run Code Online (Sandbox Code Playgroud)
当然,请将此队列用于您的URLSesson委派:
session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:myQueue];
Run Code Online (Sandbox Code Playgroud)
由于此队列设置为串行队列,因此在后台始终只有一个线程访问dict.
使用dict信息计算某些内容时要小心.您还必须在该队列上执行此操作.但是,您可以将计算结果放在任何其他队列/线程上,例如更新主线程上的UI.
[myQueue addOperationWithBlock:^
{
// Calculate with your dictionary
// Maybe the progress calcualtion
NSString* progress = [self calculateProgress: iVarDict];
dispatch_async(dispatch_get_main_queue(), ^
{
// use progress to update UI
});
}];
Run Code Online (Sandbox Code Playgroud)
我认为发布通知您不必使用该模式,因为系统正确处理线程.但要保存,你应该检查一下.
| 归档时间: |
|
| 查看次数: |
15009 次 |
| 最近记录: |