相关疑难解决方法(0)

具有NSOperations的iOS应用程序架构

两个月前我开始编写一个新的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

10
推荐指数
1
解决办法
2438
查看次数

为什么我的委托不接受performSelectorOnMainThread:withObject:waitUntilDone:?

Xcode 4给我performSelectorOnMainThread:withObject:waitUntilDone:发送给我的委托的消息的编译器警告,我不明白.

我的代表被宣布为:

@property (nonatomic, assign) id <AccountFeedbackDelegate> delegate;
Run Code Online (Sandbox Code Playgroud)

然后最终在主线程上执行:

[self.delegate performSelectorOnMainThread:@selector(didChangeCloudStatus) withObject:nil waitUntilDone:NO];
Run Code Online (Sandbox Code Playgroud)

然而Xcode仍坚持给我:

警告:语义问题:方法'-performSelectorOnMainThread:withObject:waitUntilDone:'not found(返回类型默认为'id')

当然代码编译并运行正常,但我不喜欢警告.当我像这样重新声明代表时,警告消失了,但我不喜欢这个解决方法:

@property (nonatomic, assign) NSObject <AccountFeedbackDelegate> *delegate;
Run Code Online (Sandbox Code Playgroud)

我错过了什么?我做错了什么?干杯,
EP

multithreading delegates objective-c

6
推荐指数
1
解决办法
2383
查看次数