两个月前我开始编写一个新的iPhone应用程序,因此我创建了一个通用的RESTFul Web服务,它允许我拥有许多这些必要的功能,如用户身份验证,用户配置文件,友谊系统,媒体处理,消息传递系统等.在我看来,有几个用例可以将这个web服务重用于未来的iPhone应用程序.
有了这种心态,我决定为这个应用程序(以及所有未来的应用程序)编写一个静态库,处理所有繁重的工作,如媒体(图像,视频,声音)设置和处理,与Web服务通信,解析和映射的结果,处理CoreData等.
鉴于我的应用程序,有许多并行任务正在运行的情况(最坏的情况),例如用户当前更改他/她的个人资料图片,而应用程序将用户位置发送到服务器(在后台),并且新的推送通知是接收.
因此决定在NSOperation中封装每个逻辑操作(如SendUserLocation或GetCurrentFriendList)并将它们添加到serviceQueue(NSOperationQueue).
当操作成功从Web服务获得结果并且现在应该处理它时,每个操作都能够生成子任务.

典型的ServiceManager方法如下所示
- (void)activateFriendsSync:(id)observer onSuccess:(SEL)selector {
ELOSyncFriends *opSyncFriends = [[ELOSyncFriends alloc] initWithSM:self];
[self ELServiceLogger:opSyncFriends];
[serviceQueue addOperation:opSyncFriends];
if(observer) {
[self registerObserver:observer selector:selector name:opSyncFriends.notificationName];
}
}
Run Code Online (Sandbox Code Playgroud)
每个操作,请求(到服务器)和subTask都使用GUID作为notificationName,以在完成处理时通知父对象.如果操作中的所有内容都已完成,它会将通知发送回用户界面.
也就是说,添加和删除子任务的代码看起来像这样
- (void)removeSubTask:(NSNotification*)notification {
ELRequest *request = (ELRequest*)[notification object];
[subTasks removeObjectIdenticalTo:request.notificationName];
if([subTasks count] == 0) {
// all SubTaks done, send notification to parent
[serviceManager.notificationCenter postNotificationName:self.notificationName object:request];
}
}
- (NSString*)addSubTask {
NSString* newName = [self GetUUID];
[subTasks addObject:[newName retain]];
[serviceManager.notificationCenter addObserver:self selector:@selector(removeSubTask:) name:newName object:nil];
return newName;
}
- (NSString …Run Code Online (Sandbox Code Playgroud) architecture iphone objective-c nsoperation nsoperationqueue
我喜欢iOS中的nsoperationqueue.什么是java中的等价物?
我似乎dispatch_queue_t和NSOperationQueue队列之间有一些混淆.
默认情况下,AFNetworking AFImageRequestOperation将在应用程序的主线程上执行成功回调块.要更改此设置,请使用允许您选择运行回调的队列AFHTTPRequestOperation的属性successCallbackQueue.
我正在尝试在已经执行HTTP请求的相同后台队列/后台线程上执行成功回调.NSOperationQueue运行HTTP请求的主要线程也应该运行回调,而不是返回主线程,因为我需要使用一些返回的图像进行一些繁重的计算.
我的第一次尝试是设置successCallbackQueue为运行的NSOperationQueue实例AFImageRequestOperation.但是,successCallbackQueue属性是类型的dispatch_queue_t,所以我需要一种方法来获取我的实例的底层dispatch_queue_tNSOperation,如果有这样的事情.
这是可能的,还是我需要创建一个单独的dispatch_queue_t?
我问的原因:AFNetworking继承的有些奇怪NSOperation,但我们希望我们使用dispatch_queue_t队列进行回调.混合两种范式dispatch_queue_t和NSOperationQueue.
谢谢你的任何提示!
我很喜欢,NSOperationQueue但我有一些问题需要了解它的某些部分.
在objc.io的第二期中,他们回顾了NSOperationQueue,并提到它有两种队列,即主线程上运行的主队列和后台队列.他们提到你可以访问主队列,[NSOperation mainQueue]然后添加操作它.
它还提到你可以通过创建NSOperation的实例(潜在的子类)来添加到后台队列(我理解会更好?).
mainQueue,那么如何管理向后台队列添加任务呢?它还提到您可以控制与maxConcurrentOperationCount属性同时运行的操作量.
NSOperationQueueDefaultMaxConcurrentOperationCount,但如果我手动将它设置为一个特定的数字,它是否对应于一次可以运行的最大线程数?例如,如果iPhone上的处理器可以同时运行4个线程并且我将该属性设置为8,会发生什么?我知道可以使用障碍解决GCD中的读写器问题.由于我(通常)NSOperationQueue在性能不是关键问题时尝试使用而不是GCD,我想要一个NSOperation兼容此问题的兼容解决方案.
我试着写自己的,但我的解决方案变得笨拙......当然有人已经解决了这个问题吗?
有没有人知道NSOperation兼容读写器问题的解决方案?
concurrency objective-c nsoperation nsoperationqueue grand-central-dispatch
根据Apple关于NSOperation的文档,我们必须覆盖main非并发操作的start方法和并发操作的方法.但为什么?
I\xe2\x80\x99m 目前正在迁移我的应用程序以使用 Swift 中的并发模型。我想序列化任务以确保它们一个接一个地执行(无并行性)。在我的用例中,我想监听通知中心发布的通知,并在每次发布新通知时执行任务。但我想确保之前没有任务正在运行。这相当于使用 maxConcurrentOperationCount = 1 的操作队列。
\n例如,我\xe2\x80\x99m 在我的应用程序中使用 CloudKit 和 Core Data,并使用持久历史记录来确定存储中发生了哪些更改。\n在此将本地存储同步到云示例代码中,Apple 使用用于处理历史处理任务的操作队列(在CoreDataStack中)。此OperationQueue 的最大操作数设置为1。
\nprivate lazy var historyQueue: OperationQueue = {\n let queue = OperationQueue()\n queue.maxConcurrentOperationCount = 1\n return queue\n}()\nRun Code Online (Sandbox Code Playgroud)\n当收到 Core Data 通知时,新任务将添加到该串行操作队列中。因此,如果收到多个通知,它们都会以串行方式一个接一个地执行。
\n@objc\nfunc storeRemoteChange(_ notification: Notification) {\n // Process persistent history to merge changes from other coordinators.\n historyQueue.addOperation {\n self.processPersistentHistory()\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n在此加载和显示大数据源示例代码中,Apple 使用任务来处理历史更改(在 QuakesProvider 中)。
\n// Observe Core Data remote change notifications on the queue where …Run Code Online (Sandbox Code Playgroud) core-data nsoperationqueue grand-central-dispatch swift swift-concurrency
当你使用线程时,你有任何偏好吗?一般来说,要使用以下任何一种技术:
NSOperationQueue是否简化了所有内容,因此在我们需要创建异步函数时更好用?
iphone objective-c nsthread nsoperationqueue grand-central-dispatch
cancelAllOperations()不适用于mainQueue(该cancel()方法未在该NSOperation对象上调用).我错过了什么吗?我必须遍历所有操作并调用cancel()方法才能使其工作.
我创建了一个NSOperation子类来处理一些zip存档操作.无论如何,如果我覆盖-start或者-main这段代码总是发生:
if ([NSThread isMainThread]) {
NSLog(@"I am in the main thread");
return;
}
Run Code Online (Sandbox Code Playgroud)
知道发生了什么事吗?
我试过添加这个块:
- (void) start { //also tried overriding main
if ([NSThread isMainThread]) {
NSLog(@"In main thread, trying again");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self start];
});
return;
//hard working code etc...
//cpu intensive zip operations...
}
Run Code Online (Sandbox Code Playgroud)
但这会导致崩溃,EXC_BAD_ACCESS违规指向该dispatch_async线.
objective-c nsoperation nsoperationqueue grand-central-dispatch ios
nsoperationqueue ×10
nsoperation ×6
objective-c ×6
ios ×4
concurrency ×2
iphone ×2
afnetworking ×1
architecture ×1
core-data ×1
java ×1
nsthread ×1
swift ×1