如何使用带参数的函数调用performSelectorInBackground?

Sas*_*vus 8 objective-c ios performselector

对不起新手问题(也许).我正在为ios开发一个应用程序,我正在尝试从主线程执行外部xml读取,以便在调用正在进行魔术时不冻结ui.

这是我知道使进程不在目标c的主线程中执行的唯一方法

[self performSelectorInBackground:@selector(callXml)
                           withObject:self];
Run Code Online (Sandbox Code Playgroud)

所以我把我的电话封装成一个函数

 - (void)callXml{
     [RXMLElement elementFromURL:[NSURL URLWithString:indXML]];
 }
Run Code Online (Sandbox Code Playgroud)

现在我必须使字符串indXML成为函数的参数,以便根据需要调用不同的xml.就像是

 - (void)callXml:(NSString *) name{
     [RXMLElement elementFromURL:[NSURL URLWithString:indXML]];
 }
Run Code Online (Sandbox Code Playgroud)

在这种情况下,对performSelector的调用如何改变?如果我以通常的方式做到这一点,我会得到语法错误:

[self performSelectorInBackground:@selector(callXml:@"test")
                           withObject:self];
Run Code Online (Sandbox Code Playgroud)

Mik*_*ard 15

[self performSelectorInBackground:@selector(callXml:)
                       withObject:@"test"];
Run Code Online (Sandbox Code Playgroud)

ie:你传入的作为withObject:成为你的方法的参数.

正如您感兴趣的那样,您可以使用GCD进行操作:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self callXml:@"test"];

    // If you then need to execute something making sure it's on the main thread (updating the UI for example)
    dispatch_async(dispatch_get_main_queue(), ^{
        [self updateGUI];
    });
});
Run Code Online (Sandbox Code Playgroud)


βha*_*avḯ 8

为了这

- (void)callXml:(NSString *) name{
     [RXMLElement elementFromURL:[NSURL URLWithString:indXML]];
 } 
Run Code Online (Sandbox Code Playgroud)

你可以这样打电话

  [self performSelectorInBackground:@selector(callXml:)
                               withObject:@"test"];
Run Code Online (Sandbox Code Playgroud)

如果您的方法有参数,则:与method_name一起使用,并将参数作为withObject参数传递