在后台线程中设置数据并获取bad_access错误

时间:2022-02-10 21:01:37

My goal is to initialize a large amount of data on a different (non-main) thread, and then refresh a UIProgressView on the main thread corresponding to the large data load's progress. To do this, I use performSelectorInBackground to load the data, and update the progress throughout the way using performSelectorOnMainThread.

我的目标是在不同的(非主要)线程上初始化大量数据,然后在主线程上刷新与大数据加载进度相对应的UIProgressView。为此,我使用performSelectorInBackground加载数据,并使用performSelectorOnMainThread更新整个过程中的进度。

I am currently getting a EXE_BAD_ACCESS error from one of the dictionaries that I am initializing, specifically the line where I am setting self.someDictionary = @{...}. self.someDictionary takes on strong and nonatomic properties, and is initialized on the non-main thread.

我目前正在从我正在初始化的一个词典中获取EXE_BAD_ACCESS错误,特别是我设置self.someDictionary = @ {...}的行。 self.someDictionary采用强和非原子属性,并在非主线程上初始化。

As a total newbie to multithreading, I am beginning to see that I shouldn't be setting a strong and nonatomic property (however, changing it to atomic still caused the crash). What else am I doing incorrectly to cause the EXE_BAD_ACCESS error, and how do I set a large amount of data to an NSDictionary on a non-main thread and still be able to update the progress on the main thread?

作为多线程的新手,我开始看到我不应该设置一个强大的非原子属性(但是,将其更改为原子仍会导致崩溃)。我还做了什么错误导致EXE_BAD_ACCESS错误,以及如何在非主线程上为NSDictionary设置大量数据并仍然能够更新主线程上的进度?

Thanks!

Edit 1:

Code:

//In viewWillAppear, from the main thread
[self performSelectorInBackground:@selector(populateDictionaries) withObject:nil];

//In populateDictionaries method
Dictionary *someDictionary = [[Dictionary alloc] init];

//the methods inside the Dictionary class
- (id) init{
    self = [super init];
    if (self){
        [self makeDictionaries];
    }

    return self;
}
- (void)makeDictionaries{

    self.insiderDictionary = @{ ...} //this line is causing the crash

}

1 个解决方案

#1


0  

If you want to perform the process in background try Dispatch queue

如果要在后台执行该过程,请尝试Dispatch queue

ref: https://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html

Thank you.

#1


0  

If you want to perform the process in background try Dispatch queue

如果要在后台执行该过程,请尝试Dispatch queue

ref: https://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html

Thank you.