繁体   English   中英

目标c:使用performSelectorOnMainThread时访问错误

[英]Objective c: Bad access error when using performSelectorOnMainThread

这是问题所在。

  1. 我有一个称为-(void)searchingInBackground的方法,该方法在后台运行(performSelectorInBackground)。

  2. 在这种方法中,我也有几个不同的线程也在后台运行(performSelectorInBackground)。 像这样:

     -(void)searchingInBackground { @autoreleasepool { [self performSelectorInBackground:@selector(getDuplicatedPictures:) withObject:copyArray]; } @autoreleasepool { [self performSelectorInBackground:@selector(getLocationsOfPhotos:) withObject:copyArray]; } ... (and so on) } 
  3. 在线程中的每个函数中(即getDuplicatedPictures,getLocationsOfPhotos ...),它们将在末尾生成NSString,而我将使用这些字符串来更新文本字段GUI。

  4. 为了更新我的文本字段GUI。 我创建了一个名为UpdateGUI的函数,该函数将用于帮助我更新所有NSString。 像这样,

     -(void)UpdateUI { [_NumDupPhotosLabel(label for GUI) setStringValue: resultDupPhotos(string from thread function which is getDuplicatedPictures in this case)]; ....(includes all of my strings from threads) } 
  5. 这是问题,当我在每个线程函数中使用performSelectorOnMainThread调用此UpdateGUI时。 它会给我EXC_BAD_ACCESS。 这是我所做的。 例如:

     -(void)getDupicatedPictures { resultDupPhotos = .....; [self performSelectorOnMainThread:@selector(UpdateUI) withObject:nil waitUntilDone:YES]; } 
  6. 如果我不使用performSelectorOnMainThread,只需直接在那些函数中设置值即可。 我只是想更好地组织代码。

     -(void)getDuplicatedPictures { resultDupPhotos = .....; [_NumDupPhotosLabel setStringValue: resultDupPhotos]; (works good and it will set the value to the GUI label) } 

你们能告诉我如何解决这个问题吗? 谢谢!!!

  • ARC还是没有?

  • 如果发生崩溃,请发布回溯

  • 围绕performInBackground:...使用@autoreleasepool调用无济于事( NSAutoreleasePool也无济于事-您需要将自动释放池放入执行线程中

  • 如果某个变量涉及崩溃,请显示该变量的声明和初始化

  • 同时产卵一堆线程来做一堆工作可能比顺序进行要慢。 并发应该始终受到控制。 如果你有一个长期运行的任务,你可能会想旋转了一个第二个线程。 或者您可能想重新排序操作。 但是,问题在于,一次运行多个线程,尤其是那些线程正在执行大量I / O的情况下,这只会增加争用并可能使事情变慢,通常会变慢。

在主线程尝试使用它之前,很可能会释放在后台线程上计算出的对象之一。 您如何确保resultDupPhotos在线程之间有效?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM