Iphone add task to EDT? something like SwingUtilities.invokeLater?

Is there a way to add something that needs to be done in the EDT / Event Dispatch Thread in an Iphone application, like the invokeLater method in a Java Swing application?

+3
source share
2 answers

Take a look at the NSObjectclass reference link - methods starting with performSelector

+4
source

I want to have a piece of code.

This, unfortunately, does not make the desired behavior:

- (void) invokeLater_aux:(NSArray*)functionName_arg 
{
    NSLog(@"invokeLater_aux:" );
    if(functionName_arg != nil && (functionName_arg.count > 0 )){
        // split the params:
        NSString* functionNameString = [functionName_arg objectAtIndex:0]; 
        NSLog(@"functionNameString: %@",functionNameString);      
        SEL functionName = NSSelectorFromString( functionNameString);        

        id arg = nil;
        if(functionName_arg.count > 1){
            arg = [functionName_arg objectAtIndex:1];
        }

        // call the function on main thread
        [self performSelectorOnMainThread:functionName withObject:arg waitUntilDone:YES ];    
    }
    [functionName_arg release];
}

- (void) invokeLater: (SEL)functionName withObject:(id)arg
{
    NSLog(@"invoke later: %@",NSStringFromSelector(functionName));
    [self performSelectorInBackground:@selector(invokeLater_aux:) 
                           withObject:[[NSArray alloc] initWithObjects:
                                       NSStringFromSelector(functionName),//the function name as String
                                       arg,// the function arguments
                                       nil]
     ];    
}

because it starts a new thread in invokeLater and does not wait for the main thread to complete, and invokeLater_aux will be called in the main thread (when switching context?)

n , n invokeLater.

  • invokeLater_aux, , ?
0

Source: https://habr.com/ru/post/1725390/


All Articles