Best way to performselectoronmainthread in objective c?

前端 未结 4 2171
离开以前
离开以前 2021-02-09 05:44

I\'m writing a client-server app to iPhone. And I have a question about threading. When I access my online database from the device, I need to do this on a separate thread to no

相关标签:
4条回答
  • 2021-02-09 06:11

    since you are passing instance variables, another option would be to pass self and make self thread safe.

    0 讨论(0)
  • 2021-02-09 06:27

    Well, you're already using dispatch_async. Then you should just use

         dispatch_async(dispatch_get_main_queue(),^ { ... } );
    

    from inside your background thread to perform things on the main thread. For example,

         if ([self testServerMethod])
            {
                dispatch_async(dispatch_get_main_queue(),^ {
                   [self showMessageBoxWithString: @"Server is working!"];
                   [self threadedUIActivityRemover:nil];
                } );
            }else ...
    

    It doesn't have any constraint on the number of arguments for the methods you call.

    0 讨论(0)
  • 2021-02-09 06:33

    Pass a collection, such as a dictionary of unarchived objects.

    0 讨论(0)
  • 2021-02-09 06:35

    You can also use an NSInvocation.

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