iOS应用程序架构NSOperations [英] iOS App Architecture with NSOperations

查看:227
本文介绍了iOS应用程序架构NSOperations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个月前我开始写一个新的iPhone应用程序基于这个原因,我创建了一个通用的RESTful Web服务,这让我有很多,如用户认证,用户配置文件,友谊系统,媒体处理这些必要的功能,消息系统等。在我的脑海里有几个用例重用此WebService为未来的iPhone应用程序。

Two month ago I started to write a new iPhone Application and for this reason I created a generic RESTFul web service, which allows me to have a lot of these necessary features like user authentication, user profiles, a friendship system, media processing, a messaging system and so on. In my mind there are several use cases to reuse this webservice for future iPhone applications.

通过这种心态下,我决定写这个应用程序处理所有繁重的像媒体(图片,视频,声音)的设置和处理,与Web服务通信的静态库(和所有未来的应用程序),解析和结果的映射,处理CoreData等

With this state of mind, I decided to write a static library for this application (and all future apps) that handles all the heavy lifting like media (image, video, sound) setup and processing, communicating with the web service, parsing and mapping of the results, handling CoreData and so on.

由于我的应用程序有场景时,很多并行任务运行时(最坏情况)如
用户当前改变他/她的个人资料图片,而应用程序发送用户位置服务器(后台),并获得了新的推送通知。

Given my application there are scenarios when a lot of parallel tasks are running (worst case) e.g. the user currently changes his/her profile picture, while the application sends the users location to the server (in the background) and a new push notification is received.

于是决定encapsule每个逻辑操作(如SendUserLocation或GetCurrentFriendList)中的NSOperation,并将它们添加到serviceQueue(NSOperationQueue)。

So decided to encapsule each logical operation (like SendUserLocation or GetCurrentFriendList) in a NSOperation and add them to a serviceQueue (NSOperationQueue).

每个操作能够产卵子任务在操作成功从WebService得到的结果,现在应该处理它。

Each Operation is able to spawn subtasks when the operation successfully got a result from the webservice and should process it now.

一个典型的ServiceManager方法看起来像

A typical ServiceManager method looks like

- (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]; 
    }
}

每个操作请求(该服务器)和子任务使用GUID作为notificationName当它这样做处理通知的父对象。如果在操作一切都做,它发送一个通知给用户接口

Each operation, request (to the server) and subTask uses a GUID as a notificationName to notify the parent object when it's done processing. If everything in an operation is done, it sends a notification back to the user interface.

这就是说,code添加和移除子任务看起来像这样

That said, the code for adding and removing subtasks looks like this

- (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 *)GetUUID {
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

现在我所要做的是调用的ServiceManager在我的GUI开始像

Now all I've got to do is to call the serviceManager in my gui to start a specific operation like

[self.core.serviceManager activateFriendsSync:nil onSuccess:nil];

如果我想注册一个观察者,我只是通过一个观察者对象,这样的选择

If I want to register an observer, I just pass an observer object and a selector like this

[self.core.serviceManager activateFriendsSync:self onSuccess:@selector(myMethod:)];

最后但并非最不重要,我的问题(S):将建筑运行良好,稳定的,但它是值得做的事情?它创造了太多的开销?它甚至有意义吗?如何你个人,实现并发操作?

Last but not least my question(s): The "architecture" runs very well and stable, but is it worth doing? Does it create too much overhead? Does it even make sense? How you, personally, implement concurrent operations?


亨里克

P.S。随意编辑我的问题,提出问题(作为评论),叫我的名字了这一思想。

P.S. Feel free to edit my question, ask questions (as a comment), call me names for this thinking.

我真的很难解释它,主要是因为我不是以英语为母语。不要误会我的意思。我没有写这个帖子中的任何一种炫耀。所有我想要做的就是学会(也许写了一个更先进的iphone / Objective C的问题)

I really had a hard time explaining it, basically because I'm not a native english speaker. And don't get me wrong. I didn't write this posting to show off in any kind. All I want to do is learn (and maybe to write a more advanced iphone / objective c question)

推荐答案

是的,如果它被外包给服务请求和你有很多电话要打,那么这样的图书馆不(IMO)矫枉过正,我已经写相似的东西。这种结构使得它很容易让我管理一个复杂的系统,具有非常复杂多样的任务。

yes, if it's being farmed out to service requests and you have many calls to make, then such a library is not (imo) overkill, and i have written something similar. this structure made it very easy for me to manage a complex system, with very complex and varied tasks.

主要的设计差异我做了的的使用NSNotification与服务经理。相反,我使用的青睐协议/类型回调(该操作持有引用)。 NSNotification颇重。在这种情况下,操作不保留听者(多个)/通知对象,但听众保留的操作。如果关系是1-1,然后允许取消。

the primary design difference i made was not to use NSNotification with a service manager. instead, i favored using protocols/types for callbacks (which the operation holds a reference to). NSNotification is quite heavy. in this case, the operation does not retain the listener(s)/notified objects, but the listeners retain the operation. if the relation is 1-1, then allow cancellation.

另一个主要考虑的是定义线程早期。允许客户端定义他们希望得到他们的响应哪个线程。这样做的原因是,往往是一个约束或在所通知/监听器必须更新UI回调(例如,您正在使用的UIKit或AppKit的)逻辑输入。因此,创建者可以在操作说'你必须通知本人从主线程,或我可以处理来自任何线程的响应。这会降低你的控制器/监听器/观察者code和机会大大的错误。

another major consideration is to define threading early on. allow clients to define which thread they want to receive their response on. the reason for this is that there is often a constraint or logical entry for the callback if the notified/listener must update the UI (e.g., you're using UIKit or AppKit). therefore, the creator can say to the operation 'you must inform me from the main thread', or 'i can handle the response from any thread'. this will reduce your controller/listener/observer code and chance for errors greatly.

这篇关于iOS应用程序架构NSOperations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆