Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone ActivityWeel的多线程问题_Iphone_Multithreading_Cocoa Touch_Uiactivityindicatorview - Fatal编程技术网

Iphone ActivityWeel的多线程问题

Iphone ActivityWeel的多线程问题,iphone,multithreading,cocoa-touch,uiactivityindicatorview,Iphone,Multithreading,Cocoa Touch,Uiactivityindicatorview,我对多线程处理有意见。 我得到这段代码(drawActivityWheel生成UIActivityIndicatorView): 它是有效的,但我在控制台中收到很多信息,说: *** __NSAutoreleaseNoPool(): Object 0x758a210 of class __NSArrayM autoreleased with no pool in place - just leaking *** __NSAutoreleaseNoPool(): Object 0x6e111a0

我对多线程处理有意见。 我得到这段代码(drawActivityWheel生成UIActivityIndicatorView):

它是有效的,但我在控制台中收到很多信息,说:

*** __NSAutoreleaseNoPool(): Object 0x758a210 of class __NSArrayM autoreleased with no pool in place - just leaking
*** __NSAutoreleaseNoPool(): Object 0x6e111a0 of class UIView autoreleased with no pool in place - just leaking   
*** __NSAutoreleaseNoPool(): Object 0x6e183c0 of class UISegmentedControl autoreleased with no pool in place - just leaking
我读了一些可能我应该使用的东西,比如:
[self-performSelectorOnMainThread:@selector(drawActivityWheel),对象:nil waitUntilDone:NO]

但什么也没发生。活动weel尚未显示。 我相信我使用线程的方式很糟糕,请让我知道它到底是如何工作的


谢谢你的帮助,朋友们

您需要在分离的方法中添加NSAutoreleasePool

-(void)drawActivityWheel {
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   // your threaded code here
   [pool drain];
}

您需要在分离的方法中添加NSAutoreleasePool

-(void)drawActivityWheel {
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   // your threaded code here
   [pool drain];
}

您需要实现如下线程入口点:

- (void) drawActivityWheel {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    //do work here
    [pool drain];
}

- (void) removeActivityWheel {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    //do work here
    [pool drain];
}

请注意,如果您的线程是长寿命的,则建议使用。您需要实现如下线程入口点:

- (void) drawActivityWheel {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    //do work here
    [pool drain];
}

- (void) removeActivityWheel {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    //do work here
    [pool drain];
}

请注意,如果您的线程是长寿命的,则建议使用a。

已经给出的答案充分涵盖了问题,我只想补充一点,
performSelectorOnMainThread:withObject:waitUntilDone:
不仅仅适用于与UIKit对话的任何内容,这是最需要的,因为在iOS 4.0之前,UIKit根本不是线程安全的,甚至在4.0+中,从其他线程调用也只是非常有选择地安全。在后台线程上创建
UIActivityIndicatorView
或将其添加到另一个视图中都不能保证安全。因此,您所做的操作可能会随机崩溃或在未来版本的操作系统上崩溃,或者导致任何其他未定义的行为

有关这方面的权限,请具体参见:

注意:大多数情况下,UIKit类 应仅从以下位置使用: 应用程序的主线程。这是 对于派生的类尤其如此 来自UIResponder或涉及 操纵应用程序的用户 任何方式的接口


如果在主线程上调用方法的尝试似乎没有效果,那么很可能是主线程被阻塞了。您传递的选择器将被安排尽快执行,但这只是该线程上要完成的事情列表中的另一件事。因此,如果其他东西当前正在阻塞,则在该操作完成之前,将不会执行该操作,在该操作完成时,很可能会以原子级以外的方式结束绘制和删除操作。

已经给出的答案涵盖了所提出的问题,我要补充的是,对于任何与UIKit对话的东西,
performSelectorOnMainThread:withObject:waitUntilDone:
不仅仅是首选,它主要是必需的,因为在iOS 4.0之前,UIKit根本不是线程安全的,即使在4.0+中,从其他线程调用也只是非常有选择的安全。在后台线程上创建
UIActivityIndicatorView
或将其添加到另一个视图中都不能保证安全。因此,您所做的操作可能会随机崩溃或在未来版本的操作系统上崩溃,或者导致任何其他未定义的行为

有关这方面的权限,请具体参见:

注意:大多数情况下,UIKit类 应仅从以下位置使用: 应用程序的主线程。这是 对于派生的类尤其如此 来自UIResponder或涉及 操纵应用程序的用户 任何方式的接口

如果在主线程上调用方法的尝试似乎没有效果,那么很可能是主线程被阻塞了。您传递的选择器将被安排尽快执行,但这只是该线程上要完成的事情列表中的另一件事。所以,如果其他东西当前正在阻塞,那么在该操作完成之前,它将不会被执行,在这一点上,您很可能会最终以原子方式进行绘制和删除