ios - 立即停止方法/对象?

标签 ios xcode multithreading

这是我关于 stackoverflow 的第一个问题。我正在开发一个 iOS 应用程序,该应用程序使用来自网络上 MySQL 服务器的数据。我创建了一个名为“DataController”的类,它完全管理同步过程,并使用 NSURLConnection 和委托(delegate)来检索信息、解析它并将其存储在 CoreData 模型中。它在此过程中使用了多种方法,如下所示:

[self.dataControllerObject syncStudents]

syncStudents is called
--> gets download-list from server
--> stores IDs for all elements that have to be downloaded in NSArray-property
--> calls syncNextStudent

syncNextStudent is called
--> gets first element from NSArray-property
--> builds up NSURLConnection to retrieve data

connectionDidFinishLoading is called
--> data is stored in CoreData
--> ID is removed from NSArray-property
--> calls syncNextStudent

syncNextStudent eventually has no array elements left and finishes the process.

我希望我把功能说清楚了。现在这是我的问题:

如何中止整个过程,例如当用户不想现在同步并点击某个按钮时?

我尝试创建 DataController 对象并使用 [self performSelectorInBackground:@selector(startSyncing) withObject:nil] 在另一个线程中调用 syncStudents 方法,但现在我的 NSURLConnection 没有触发任何委托(delegate)方法。

我能做什么?

提前致谢。

最佳答案

您应该考虑使用 NSOperationNSOperationQueue 而不是 performSelectorInBackground:。这使您可以更好地控制需要在后台执行的一批任务以及一次取消所有操作。这是我的建议。

NSOperationQueue 声明为属性

@property (nonatomic, retain) NSOperationQueue *operationQueue;

然后在你的实现文件中实例化它:

_operationQueue = [[NSOperationQueue] alloc] init];

创建一个将执行处理的 NSOperation 派生类。

@interface StudentOperation : NSOperation

// Declare a property for your student ID
@property (nonatomic, strong) NSNumber *studentID;

@end

然后遍历创建操作所需的任何集合。

for (NSSNumber *studentID in studentIDs) { // Your array of ids
    StudentOperation *operation = [[StudentOperation alloc] init];

    // Add any parameters your operation needs like student ID
    [operation setStudentID:studentID];

    // Add it to the queue
    [_operationQueue addOperation:operation]; 
}

当你想取消时,只需要告诉操作队列:

[_operationQueue cancelAllOperations];

请记住,这将立即取消所有当前未处理的排队操作。如果你想停止当前正在运行的任何操作,你必须将代码添加到你的 NSOperation 派生类(StudentOperation 上面)来检查这个。因此,假设您的 NSOperation 代码正在运行其 main() 函数。您需要定期检查并查看是否设置了 cancelled 标志。

@implementation StudentOperation

- (void)main
{
    // Kick off your async NSURLConnection download here.
    NSURLRequest *theRequest = [NSURLRequest requestWithURL...

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    // ...
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the data
    [receivedData appendData:data];

    // Check and see if we need to cancel
    if ([self isCancelled]) {
        // Close the connection. Do any other cleanup

    }
}

@end

关于ios - 立即停止方法/对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14855504/

相关文章:

iphone - 远程更新 iOS 资源文件

ios - 为什么我从 S3 下载的图像只出现在 1 个人的手机上?

ios - 通过提交增加版本号

java - java system.in可以在不使用线程的情况下同时与输入源代码一起工作吗

c# - C#中的异步操作和线程

ios - 删除 keyValue 观察者时崩溃(使用 Apple SquareCam 示例代码)

iphone - 应用程序进入后台状态时如何取消 AVExportSession?

ios - 从 Popover 关闭或弹出到 Root View Controller

swift - UITextField - 如果文本字段包含某些单词,则不发送到 firebase

c++ - 如何强制线程严格按顺序工作?