检测performSelectorInBackground:withObject的结束:

发布于 2024-10-25 22:18:20 字数 150 浏览 1 评论 0原文

我的 iOS 应用程序中有一个异步服务器请求:

[self performSelectorInBackground:@selector(doSomething) withObject:nil];

如何检测此操作的结束?

I have an asynchronous server request in my iOS app:

[self performSelectorInBackground:@selector(doSomething) withObject:nil];

How can I detect the end of this operation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

偏闹i 2024-11-01 22:18:20

在 doSomething 方法的末尾放置一个调用?!

- (void)doSomething {
    // Thread starts here

    // Do something

    // Thread ends here
    [self performSelectorOnMainThread:@selector(doSomethingDone) withObject:nil waitUntilDone:NO];
}

Put a call at the end of the doSomething method?!

- (void)doSomething {
    // Thread starts here

    // Do something

    // Thread ends here
    [self performSelectorOnMainThread:@selector(doSomethingDone) withObject:nil waitUntilDone:NO];
}
草莓酥 2024-11-01 22:18:20

如果您只是想知道它何时完成(并且不想传回太多数据 - 此时我会推荐一个委托),您可以简单地向通知中心发布通知。

[[NSNotificationCenter defaultCenter] postNotificationName:kYourFinishedNotificationName object:nil];

在视图控制器 viewDidLoad 方法中添加:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(yourNotificationListenerMethod:)
                                                 name:kYourFinishedNotificationName
                                               object:nil];

并在 dealloc 中添加:

[[NSNotificationCenter defaultCenter] removeObserver:self];

If you just want to know when it's finished (and don't want to pass much data back - at which point I would recommend a delegate) you could simply post a notification to the notification center.

[[NSNotificationCenter defaultCenter] postNotificationName:kYourFinishedNotificationName object:nil];

within your view controllers viewDidLoad method add:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(yourNotificationListenerMethod:)
                                                 name:kYourFinishedNotificationName
                                               object:nil];

and within dealloc, add:

[[NSNotificationCenter defaultCenter] removeObserver:self];
简单 2024-11-01 22:18:20

您可能希望让您的 doSomething 选择器在完成后返回。 PerformSelectorInBackground 上发生的事情是 API 内部的,如果您想要更多控制,您可能需要使用performSelector:onThread:withObject:waitUntilDone:并定期检查线程上的isFinished通过了。我确信您更关心 doSomething 的完成而不是实际的线程,所以只需从那里发回即可。

You may want to have your doSomething selector post back when it has completed itself. What happens on performSelectorInBackground is internal to the API and if you want more control you may want to use performSelector:onThread:withObject:waitUntilDone: and periodically check the isFinished on the thread passed. I am sure you are more concerned about doSomething finishing than the actual thread though so just post back from there.

淡紫姑娘! 2024-11-01 22:18:20

我创建了一个通用后台处理程序来解决这个问题。只有第一个方法是公共的,因此可以在整个过程中使用。请注意,所有参数都是必需的。

+(void) Run: (SEL)sel inBackground: (id)target withState: (id)state withCompletion: (void(^)(id state))completionHandler       // public
{
    [(id)self performSelectorInBackground: @selector(RunSelector:) withObject: @[target, NSStringFromSelector(sel), state, completionHandler]];
}

+(void) RunSelector: (NSArray*)args
{
    id target = [args objectAtIndex: 0];
    SEL sel = NSSelectorFromString([args objectAtIndex: 1]);
    id state = [args objectAtIndex: 2];
    void (^completionHandler)(id state) = [args objectAtIndex: 3];
    [target performSelector: sel withObject: state];
    [(id)self performSelectorOnMainThread: @selector(RunCompletion:) withObject: @[completionHandler, state] waitUntilDone: true];
}

+(void) RunCompletion: (NSArray*)args
{
    void (^completionHandler)(id state) = [args objectAtIndex: 0];
    id state = [args objectAtIndex: 1];
    completionHandler(state);
}

下面是它的调用方式的示例:

NSMutableDictionary* dic = [[NSMutableDictionary alloc] init];
__block BOOL done = false;
[Utility Run: @selector(RunSomething:) inBackground: self withState: dic withCompletion:^(id state) {
        done = true;
    }];

I have a general background handler I created that solves this problem. Only the first method is public so it can be used throughout. Note that all parameters are required.

+(void) Run: (SEL)sel inBackground: (id)target withState: (id)state withCompletion: (void(^)(id state))completionHandler       // public
{
    [(id)self performSelectorInBackground: @selector(RunSelector:) withObject: @[target, NSStringFromSelector(sel), state, completionHandler]];
}

+(void) RunSelector: (NSArray*)args
{
    id target = [args objectAtIndex: 0];
    SEL sel = NSSelectorFromString([args objectAtIndex: 1]);
    id state = [args objectAtIndex: 2];
    void (^completionHandler)(id state) = [args objectAtIndex: 3];
    [target performSelector: sel withObject: state];
    [(id)self performSelectorOnMainThread: @selector(RunCompletion:) withObject: @[completionHandler, state] waitUntilDone: true];
}

+(void) RunCompletion: (NSArray*)args
{
    void (^completionHandler)(id state) = [args objectAtIndex: 0];
    id state = [args objectAtIndex: 1];
    completionHandler(state);
}

Here's an example of how it's called:

NSMutableDictionary* dic = [[NSMutableDictionary alloc] init];
__block BOOL done = false;
[Utility Run: @selector(RunSomething:) inBackground: self withState: dic withCompletion:^(id state) {
        done = true;
    }];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文