iOS performSelectorOnMainThread with multiple arguments

后端 未结 3 1839
北荒
北荒 2021-02-01 01:46

I would like to perform a selector on the main thread from another thread, but the selector has multiple arguments, similar to this:

-(void) doSomethingWith:(int)

相关标签:
3条回答
  • 2021-02-01 02:10

    You'll need to use a NSInvocation

    Create the object, set the target, selector and arguments.
    Then, use

    [ invocationObject performSelectorOnMainThread: @selector( invoke ) withObject: nil, waitUntilDone: NO ];
    
    0 讨论(0)
  • 2021-02-01 02:26

    When you're using iOS >= 4, you'd do this instead:

    dispatch_async(dispatch_get_main_queue(), ^{
        [self doSomething:1 b:2 c:3 d:4 e:5];
    });
    

    That's like doing waitUntilDone:NO. If you want to wait until the method is finished, use dispatch_sync instead.

    0 讨论(0)
  • 2021-02-01 02:27

    you can pass one object of NSDictionary/NSArray type having required arguments.

    and accept the same type of object in your function. then, decompose the values and proceed with processing.

    you have to use NSNumber for numeric values for adding them to NSarray/NSDictionary and later on in your function, you can convert them back with intValue/floatValue etc

    best of buck.

    0 讨论(0)
提交回复
热议问题