1 year ago
#379204
NedMed
Objective C Threading using GCD Without Memory Leak
In iOS and using Objective C and Grand Central Dispatch (GCD) I want to fetch data on a background thread then return with data to the main thread, but am having trouble with memory leaks. Could someone please help me change the below code to not have memory leaks. I think I need to use "weak self" somehow?
- (void) startLoadData { // called from main thread
dispatch_queue_t qBGThread = dispatch_queue_create("MyApp.loadDataBGThread", NULL);
dispatch_async(qBGThread, ^ { [self loadSomeDataOnBGThread]; });
}
- (void) loadSomeDataOnBGThread {
MyResponse *response = [DataFetcher getSomeData];
dispatch_async(dispatch_get_main_queue(), ^{[self loadDataFinished: response];});
}
- (void) loadDataFinished:(MyResponse *)response {
// back on main thread
if (response.successful) {
[self displayData: response]
} else {
// handle data fetch error
}
}
ios
objective-c
multithreading
grand-central-dispatch
0 Answers
Your Answer