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

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

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

[self performSelectorInBackground:@selector(callXml)
                           withObject:self];

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

- (void)callXml{
     [RXMLElement elementFromURL:[NSURL URLWithString:indXML]];
 }

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

- (void)callXml:(Nsstring *) name{
     [RXMLElement elementFromURL:[NSURL URLWithString:indXML]];
 }

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

[self performSelectorInBackground:@selector(callXml:@"test")
                           withObject:self];

解决方法

[self performSelectorInBackground:@selector(callXml:)
                       withObject:@"test"];

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];
    });
});

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...