GCD - 完成其他方法后按顺序执行方法
我有一个类,我使用不同的数据多次调用该类。
该类调用 Web 服务,解析它对 NSDictionary 的响应,并将数据保存在 Core Data 上。
Web服务的调用和核心数据的保存是在不同的线程中完成的,使用核心数据队列,以便UI保持响应。
类:
- (void)refreshDataFromWebService:(NSString *)webserviceWSDL
{
dispatch_queue_t receiveActivities = dispatch_queue_create("com.myApp.ws.wsdlMethod", NULL);
dispatch_async(receiveData, ^(void)
{
//call web service
//...
//parse received data to NSDictionary
//...
});
dispatch_release(receiveData);
}
//some work
//the class that works with the WS, calls a method on it's delegate, and the saveData is called.
- (void)saveData
{
dispatch_queue_t request_queue = dispatch_queue_create("com.myApp.insertDataOnCoreData", NULL);
dispatch_async(request_queue, ^{
//save data to CoreData with new Manage Object Context
//...
//...
});
dispatch_release(request_queue);
}
问题是我需要按某种顺序调用该类大约 15 次。
最好的方法是什么?
我应该打电话:
[SomeClass refreshDataFromWebService:method_1];
[SomeClass refreshDataFromWebService:method_2];
[SomeClass refreshDataFromWebService:method_3];
[SomeClass refreshDataFromWebService:method_4];
还是应该采取不同的方式?
目标是,由于关系,method_2 仅在 method_1 完成在 CoreData 上的保存后调用。
感谢您的宝贵帮助,
鲁伊·洛佩斯
I have a class that I call several times with different data.
That class, calls a web-service, parse it's response to NSDictionary, and save the data on Core Data.
The call of the web service and the saving in core data are done in different threads, using core data queues, so that the UI keeps responsive.
Class:
- (void)refreshDataFromWebService:(NSString *)webserviceWSDL
{
dispatch_queue_t receiveActivities = dispatch_queue_create("com.myApp.ws.wsdlMethod", NULL);
dispatch_async(receiveData, ^(void)
{
//call web service
//...
//parse received data to NSDictionary
//...
});
dispatch_release(receiveData);
}
//some work
//the class that works with the WS, calls a method on it's delegate, and the saveData is called.
- (void)saveData
{
dispatch_queue_t request_queue = dispatch_queue_create("com.myApp.insertDataOnCoreData", NULL);
dispatch_async(request_queue, ^{
//save data to CoreData with new Manage Object Context
//...
//...
});
dispatch_release(request_queue);
}
The issue is that I need to call this Class about 15 times, and in some order.
What is the best way to do it?
Should I call:
[SomeClass refreshDataFromWebService:method_1];
[SomeClass refreshDataFromWebService:method_2];
[SomeClass refreshDataFromWebService:method_3];
[SomeClass refreshDataFromWebService:method_4];
or should I do a different way?
The goal is that method_2 is only called after method_1 is finish saving on CoreData, due to relationships.
Thanks for you precious help,
Rui Lopes
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果接收数据的时间比调用保存数据的时间长,那么您第一次调用接收数据然后保存数据的情况将不起作用,而这种情况几乎总是如此。最后需要在接收块内部调用保存操作。现在,为了一次调用一个服务,您应该为 该类的串行调度队列,并使用它,并且仅在 dealloc 方法中释放它。另一种选择是使用
NSOperation
s,队列的最大并发操作设置为 1。Your first call to receive data and then save data the will not work in any scenario where it takes longer to receive data than it does to call save the data, which would likely almost always be the case. The save operation needs to be called inside of the receive block at the end. Now for the services to be called one at a time in order you should create an ivar for a serial dispatch queue for the class and use that and only release it in the dealloc method. Another option is to use
NSOperation
s with a queue that has max concurrent operations set to 1.为什么不简单地为类的每个实例创建一个串行队列,然后针对内部队列序列化操作?这将确保 method_2 在 method_1 之后发生(假设 method_1 按照设计首先入队),同时仍然允许该类的所有实例彼此并行运行。当然,这是假设这是您的目标 - 很难从有问题的代码片段中看出。
Why don't you simply create a single serial queue for each instance of your class and then serialize the operations against the internal queue? This will ensure that method_2 happens after method_1 (assuming that method_1 was enqueue first, by design) while still allowing all instances of the class to run in parallel with respect to one another. This assumes, of course, that this is your goal - it's hard to tell from the code fragment in question.