Using GCD in performSelectorInBackground: , How this action get an exception? - ios

When I call "performSelectorInBackground" to process some action asynchronously.
My code struct is like:
[self performSelectorInBackground:#selector(AddTheAdditionalDataSourceToTableView) withObject:nil];
and AddTheAdditionalDataSourceToTableView's code struct is like :
-(void)AddTheAdditionalDataSourceToTableView
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
...
dispatch_async(dispatch_get_current_queue(), ^{
...
});
...
});
}
and after the viewController call 'popViewController', dealloc will never be called!!!
I know I can just call 'AddTheAdditionalDataSourceToTableView' directly. it will process asynchronously and it works fine. dealloc will called when the viewController did disappear.But I don't know why calling it using "performSelectorInBackground" will cause this exception above.
: (
- (void)reloadTableViewDataSource
{
// should be calling your tableviews data source model to reload
// put here just for demo
[self performSelectorInBackground:#selector(reFreshTableView) withObject:nil];
//[self reFreshTableView];
}
- (void)reFreshTableView
{
NSString *myPoint = [self currentLocationPoint];
if (!myPoint || [myPoint isEqualToString:#""]) {
[self enableButtons];
[tableView tableViewDidFinishedLoading];
return;
}
Interface *interface = [[Interface alloc]init];
/* 默认从page 1开始刷新数据 */
[self disableButtons];
_interface = (id)interface;
interface.delegate = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[interface aroundInfoFromServiceInterface:_distanceType
withTableType:_tableType
withSortType:_sortType
withPageNum:1
withNumPerPage:NumberOfCellInOneRequest withPoint:myPoint];
});
//[interface release];
//[tableView flashMessage:#"hahahah"];
}
- (void)aroundInfoFromServiceInterface:(NSInteger)distance
withTableType:(NSInteger)tableType
withSortType:(NSInteger)sortType
withPageNum:(NSInteger)pageNum
withNumPerPage:(NSInteger)numPerPage
withPoint:(NSString *)myPiont
{
RequestData *data1 = [RequestData requestData:[NSString stringWithFormat:#"%d", distance] key:#"aroundDistant"];
RequestData *data2 = [RequestData requestData:[NSString stringWithFormat:#"%d", tableType] key:#"itemType"];
RequestData *data3 = [RequestData requestData:[NSString stringWithFormat:#"%d", sortType] key:#"sortType"];
RequestData *data4 = [RequestData requestData:[NSString stringWithFormat:#"%d", pageNum] key:#"pageNum"];
RequestData *data5 = [RequestData requestData:[NSString stringWithFormat:#"%d", numPerPage] key:#"numPerPage"];
RequestData *data6 = [RequestData requestData:[NSString stringWithFormat:#"%#", myPiont] key:#"mappoint"];
NSLog(#"%# , %# , %# , %# , %# , %#" , [NSString stringWithFormat:#"%d", distance] ,[NSString stringWithFormat:#"%d", tableType] ,[NSString stringWithFormat:#"%d", sortType] , [NSString stringWithFormat:#"%d", pageNum], [NSString stringWithFormat:#"%d", numPerPage] , [NSString stringWithFormat:#"%#", myPiont]);
NSArray *tempArray = [NSArray arrayWithObjects:data1, data2, data3, data4, data5, data6, nil];
DataFromService *dataFromService = [[[DataFromService alloc]init]autorelease];
dispatch_async(dispatch_get_main_queue(), ^{
NSString *strContent = [dataFromService requestData:tempArray fromURL:tableType == 0?AROUND_FRIEND_INFO_URL:AROUND_FISHING_STORE_AND_AREA_INFO_URL];
NSArray *responseArray = (NSArray *)[self checkRequestResponse:strContent];
if (responseArray) {
NSMutableArray *tempArray = [[NSMutableArray alloc]init];
switch (tableType) {
case TableType_FrinedsAround:
tempArray = [[FormatData shareInstance] formatDictToFriendsAround:responseArray];
break;
case TableType_FishingStoreAround:
tempArray = [[FormatData shareInstance] formatDictToFishingStoreAround:responseArray];
break;
case TableType_FishingAreaAround:
tempArray = [[FormatData shareInstance] formatDictToFishingAreaAround:responseArray];
break;
default:
break;
}
if (self.delegate && [self.delegate respondsToSelector:#selector(requestDataSuccess:responses:)]) {
[self.delegate requestDataSuccess:self responses:tempArray];
return;
}
}
});
}

dispatch_get_current_queue()
Gets a background queue, because you are using performSelectorInBackground, it gets called from a background queue.
dispatch_get_main_queue()
dispatch_async returns immediately, so you don't need to use performSelectorInBackground:. You can just call dispatch_async directly:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
...
dispatch_async(dispatch_get_main_queue(), ^{
...
});
...
});

Related

Messages deleted in chat room re-appear after re-enter the chat room

This below code is fired when I press delete button after selecting messages I want to delete in chat room.
- (void)deleteButtonPressed:(id)sender {
if (arrayToDelete.count) {
for (NSString *str in arrayToDelete) {
NSLog(#"msgID --> %#",str);
[self.chatModel.dataSource removeObject:str]; //??? Remove data from the screen
[[FMDBManager sharedInstance] deleteMessageByMessageId:str]; //??? Delete data from database
}
[arrayToDelete removeAllObjects];
[self.chatTableView reloadData];
}
}
This line successfully removes selected messages from the chat room.
[self.chatModel.dataSource removeObject:str]; //??? Remove data from the screen
When I go out the chat room and re-enter, those messages still exist, so I have this line below.
[[FMDBManager sharedInstance] deleteMessageByMessageId:str]; //??? Delete data from database
I think the above line should delete those selected messages from the database but when I re-enter the chat room I still see those messages. Here below are related code to that.
- (void)deleteMessageByMessageId:(NSString *)messageId {
FMDatabase *db = [self getterDataBase];
[db open];
NSString *sqlString = [NSString stringWithFormat:#"DELETE FROM message WHERE messageId = '%#'",messageId];
BOOL status = [db executeUpdate:sqlString];
NSLog(#"Delete MessageById:%# Status:%d",messageId,status);
[db close];
}
I've found that when chat room calls viewDidLoad it will eventually call the method callBackGetChannelLogNew where server will sync-up data with chat room tableview and local database.
- (void)callBackGetChannelLogNew:(NSDictionary *)resultDataDic status:(enumAPI_STATUS)eAPI_STATUS {
if (isFirstTimeUpdate) {
}
if (eAPI_STATUS == API_STATUS_SUCCEE) {
NSString *readString=[NSString stringWithFormat:#"%#",resultDataDic[#"read_arr"]];
if ([readString isEqualToString:#""]) {
// NSLog(#"read_arr is empty");
}
else {
NSArray *read_arr=resultDataDic[#"read_arr"];
// Copy read_arr
self.readArray=[read_arr mutableCopy];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{
[self dealWithReadArray:read_arr];
});
}
NSArray *data = [resultDataDic objectForKey:#"msg"];
if (data.count > 0) {
apiHaveData = YES;
} else {
apiHaveData = NO;
self.loadIngView.hidden = YES;
isLoadingData = NO;
return;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{
// Reverse order of data
NSArray* reversedArray = [[data reverseObjectEnumerator] allObjects];
NSMutableArray *messageFromOtherArray = [NSMutableArray new];
NSMutableArray *messageAllArray = [NSMutableArray new];
for (int i = 0; i < reversedArray.count; i++) {
NSDictionary *_dic = reversedArray[i];
NSString *fromId = [_dic objectForKey:#"fid"];
NSString *message = [NSString stringWithFormat:#"%#",[_dic objectForKey:#"say"]];
if ([ObjectManager getChatMessageKindWithString:message] == MessageTypeText) {
message = [ObjectManager decryptWithString:message];
}
NSString *messageId = [_dic objectForKey:#"mid"];
NSString *toId = [_dic objectForKey:#"tid"];
NSDateFormatter *_formatter = [[NSDateFormatter alloc] init];
_formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss.SSS";
NSDate *date_t = [NSDate dateWithTimeIntervalSince1970:[[_dic objectForKey:#"t"] doubleValue]/1000.0]; //換算成日期
NSString *stringDate = [_formatter stringFromDate:date_t];
NSString *sendDate = stringDate;
NSString *lid = _dic[#"lid"];
NSMutableDictionary *myDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
fromId,#"fromId",
message,#"message",
messageId,#"messageId",
sendDate,#"sendDate",
toId,#"toId",
lid,#"lid",
nil];
NSString *isRead;
if (_chatRoomType == ChatRoomTypePrivate) {
if ([_dic[#"r"] intValue]) {
isRead = #"1";
myDic[#"isRead"] = isRead;
lastReadMessageId = [NSString stringWithFormat:#"%#",messageId];
}
}
if (i == 0) {
if (lidForAPI != [_dic[#"lid"] intValue]) {
lidForAPI = [_dic[#"lid"] intValue];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
apiHaveData = NO;
self.loadIngView.hidden = YES;
isLoadingData = NO;
});
return ;
}
}
if (![myDic[#"fromId"] isEqualToString:[User sharedUser].account]) {
[messageFromOtherArray addObject:myDic];
}
if (_chatRoomType == ChatRoomTypeGroup) {
[myDic setObject:#"1" forKey:#"isGroupMessage"];
}
[myDic setObject:#"1" forKey:#"did_I_Read"];
[messageAllArray addObject:myDic];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self setupViewWithMessageArray:messageAllArray]; //???? Here server sync-up data with tableview
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
if (_chatRoomType == ChatRoomTypePrivate) {
if (messageFromOtherArray.count > 0 && isUplaodLastRead == NO) {
isUplaodLastRead = YES;
NSDictionary *lastReadMsgDic = messageFromOtherArray.lastObject;
[self callMsgReadAPI:lastReadMsgDic];
}
} else {
if (messageAllArray.count > 0 && isUplaodLastRead == NO) {
isUplaodLastRead = YES;
NSDictionary *lastReadMsgDic = messageAllArray.lastObject;
[self callMsgReadAPI:lastReadMsgDic];
}
}
self.chatModel.channelTopic = _topic;
NSArray *read_arr=resultDataDic[#"read_arr"];
[self dealMySendMessageReadedWithReadArray:read_arr AndMessageArray:messageAllArray];
[self saveMessageWithArray:messageAllArray]; //???? Here server sync-up data with local db
});
});
}
}
This lines will sync-up data from server to tableview
dispatch_async(dispatch_get_main_queue(), ^{
[self setupViewWithMessageArray:messageAllArray]; //???? Here server sync-up data with tableview
});
Here below is the method setupViewWithMessageArray
- (void)setupViewWithMessageArray:(NSArray *)messageAllArray {
if (!isFirstTimeUpdate) {
isFirstTimeUpdate = YES;
self.chatModel.dataSource = nil;
[self.chatTableView reloadData];
self.chatModel.dataSource = [[NSMutableArray alloc] init];
[self addMessageWithArray:messageAllArray];
[self.chatTableView reloadData];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.chatModel.dataSource.count-1 inSection:0];
[self.chatTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
} else {
[self addMessageWithArray:messageAllArray];
[self reloadTableViewWithoutMove];
}
self.loadIngView.hidden = YES;
isLoadingData = NO;
if (_chatRoomType == ChatRoomTypePrivate) {
if (lastReadMessageId) {
[self.chatModel setPrivateChatListAllReadFormMessageId:lastReadMessageId];
}
}
}
This line will sync-up data from server to local db
[self saveMessageWithArray:messageAllArray]; //???? Here server sync-up data with local db
Here below is the method saveMessageWithArray
- (void)saveMessageWithArray:(NSArray *)messageArray {
for (NSDictionary *myDic in messageArray) {
if (![[FMDBManager sharedInstance] didMessageExistWithMessageID:[myDic objectForKey:#"messageId"]]) {
[[FMDBManager sharedInstance] SaveMessage:myDic];
}
else {
NSString *mid=[NSString stringWithFormat:#"%#",myDic[#"messageId"]];
NSString *isRead = myDic[#"isReaed"];
if (isRead) {
[[FMDBManager sharedInstance] UpdateisReadWithMessageID:mid];
}
}
}
}
So I think now my question is how I can update messageAllArray with arrayToDelete before server sync-up?

network activity indicator not appearing in iOS app

In my iOS app, I want to show the network activity indicator in the top status bar.
I've added the following:
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
But the activity indicator never appears.
Does anyone know what might be wrong?
Here is the full code:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
// load sets
[self loadSets];
}
-(void)loadSets{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"in loadSets");
// show loading animation
UIView *loadingView = loadingIndicator;
loadingView.center = CGPointMake(screenWidth/2, screenHeight/2);
[self.view addSubview:loadingView];
[loadingIndicator startAnimating];
self.userSets = [[NSMutableArray alloc]init]; // re-initialize userSets
dispatch_async(bgQueue, ^{
NSString *userURLString = [userBaseUrl stringByAppendingFormat:#"/%#.json?auth_token=%#", username, auth_token];
NSLog(#"userURLString %#", userURLString);
NSURL *userURL = [NSURL URLWithString:userURLString];
NSData * userData = [NSData dataWithContentsOfURL:userURL];
dispatch_async(dispatch_get_main_queue(), ^{
if(userData){
[self fetchSets:userData];
// remove loading animation
[loadingView removeFromSuperview];
}else{
// error with authentication - should log out and require relogin
// [self logoutClick];
}
});
});
});
}
-(void)fetchSets:(NSData *)responseData{
NSError * error;
NSDictionary * json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
if(json){
NSArray *sets = [json objectForKey:#"sets"];
for (NSDictionary *currentSet in sets){
Set *userSet = [[Set alloc] init];
userSet.name = [currentSet objectForKey:#"name"];
userSet.videoURL = [[currentSet objectForKey:#"media"] objectForKey:#"mp4"];
userSet.gifURL = [[currentSet objectForKey:#"media"] objectForKey:#"gif"];
userSet.imgURL = [[currentSet objectForKeyedSubscript:#"media"] objectForKey:#"img"];
userSet.setID = [currentSet objectForKey:#"id"];
[self.userSets addObject: userSet];
}
NSLog(#"trying to reload table data with userSets length %d", [self.userSets count]);
[self.collectionView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"done loading table data");
});
}
}

Performing operations on background threads and update the UI from a different class

I have a class called APICalls that manages the calls to the API. Every View Controller calls the appropriate method (createUsername, getStates...) and pass the parameters required. When the data is received and parsed, it calls back the viewcontroller to update the UI with the info downloaded. The following code is working but I would like to know if there is an easier or more flexible/appropriate way of doing this, specially when I update the UI in the viewcontroller. Perhaps with protocols and delegates? Any suggestion is welcomed.
-(void) getObjects:(id)returnObject ofClass:(Class)returnClass fromUrl:(NSString *)urlString withPost:(NSString *)post orPut:(NSString *)put token:(NSString *)token callName:(NSString *)call andAlertTitle:(NSString *)alertTitle
{
// NSString *className = NSStringFromClass([object class]);
__block NSObject *object = returnObject;
__block Class class = returnClass;
__block NSMutableArray *array = [[NSMutableArray alloc]init] ;
__block BOOL dataReceived = NO;
[SVProgressHUD showWithStatus:#"Connecting to the server"];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
ServerConnection *sc = [[ServerConnection alloc] init]; //post/get/put
NSDictionary *jsonDict;
if ( ([post isEqualToString:#""] || !post ) && ([put isEqualToString:#""] || !put ) )
jsonDict = [sc getFromUrl:urlString withToken:token];
else if ([put isEqualToString:#""] || !put)
jsonDict = [sc postToUrl:urlString withPost:post andToken:token];
else
jsonDict = [sc putToUrl:urlString withPut:put andToken:token];
if (jsonDict)
{
NSLog(#"API: json received");
//parse the received json
NSObject *data = [self parseJson:jsonDict alertTitle:alertTitle];
if ([data isKindOfClass:[NSArray class]]) {
NSLog(#"API: Array");
dataReceived = YES;
// Iterate through the array of dictionaries
for(NSDictionary *dict in (NSArray *) data) {
object = [[class alloc] initWithJSONDictionary:dict];
[array addObject:object];
}
}
else if ([data isKindOfClass:[NSDictionary class]]){
NSLog(#"API: Dictionary");
dataReceived = YES;
object = [[class alloc] initWithJSONDictionary:(NSDictionary *)data];
if ([array count]> 0)
[array addObject:object];
}
else
NSLog(#"API: Error from API"); //alertview is shown from HandleError class
}
else{
NSLog(#"no json received");
dispatch_async(dispatch_get_main_queue(), ^(void){
[self alertStatus:#"Error when connecting to the server, please try it again" :alertTitle];
});
}
if (dataReceived)
{
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
[SVProgressHUD dismiss];
if ([call isEqualToString:#"getStates"])
{
if ([self.currentViewController isKindOfClass:[SignUpViewController class]])
{
SignUpViewController *signup = (SignUpViewController *) self.currentViewController;
[signup updateStatesList:array];
}
else if ([self.currentViewController isKindOfClass:[MyProfileViewController class]])
{
MyProfileViewController *profileVC = (MyProfileViewController *) self.currentViewController;
[profileVC updateStatesList:array];
}
}
else if ([call isEqualToString:#"getPoints"])
{
PromotionSelectionViewController *promotionVC = (PromotionSelectionViewController *) self.currentViewController;
[promotionVC updatePoints:object];
}
else if ([call isEqualToString:#"getPromotions"])
{
PromotionSelectionViewController *promotionVC = (PromotionSelectionViewController *) self.currentViewController;
[promotionVC updatePromotionsList:array];
}
});
}
});
}
//CreateUser: creates an user when this sign up
-(void)createUserWithUsername:(NSString *)username name:(NSString *)name surname:(NSString *)surname birthdate:(NSString *)birthdate address:(NSString*) address city:(NSString *)city state:(int)state country:(int)country zipCode:(int)zipCode email:(NSString *)email password:(NSString *)password fromViewController:(UIViewController *)currentViewController
{
self.currentViewController = currentViewController;
//Create the post with the username and password
NSString *post =[[NSString alloc] initWithFormat:#"username=%#&name=%#&surname=%#&address=%#&city=%#&state=%d&country=%d&zipcode=%d&birthdate=%#&email=%#&password=%#&",username, name, surname, address, city, state, country, zipCode, birthdate,email,password];
NSLog(#"post: %#", post);
User *user;
[self getObjects:user ofClass:NSClassFromString(#"User") fromUrl:signupURL withPost:post orPut:nil token:nil callName:#"createUser" andAlertTitle:#"SignUp Failed"];
}
-(void) getPointsWithToken:(NSString *)token fromViewController:(UIViewController *)currentViewController{
self.currentViewController = currentViewController;
[self getObjects:nil ofClass:nil fromUrl:getPointsURL withPost:nil orPut:nil token:token callName:#"getPoints" andAlertTitle:#"Get Proints Number Failed"];
}
-(void)getStatesforCounry:(int)idCountry fromViewController:(UIViewController *) currentViewController
{
self.currentViewController = currentViewController;
NSString *url = [NSString stringWithFormat:#"%#%d", getStatesURL, idCountry];
// NSLog(#"url: %#", url);
State *state;
[self getObjects:state ofClass:NSClassFromString(#"State") fromUrl:url withPost:nil orPut:nil token:nil callName:#"getStates" andAlertTitle:#"States not loaded"];
}
...
Using a delegate protocol pattern might help, but in this situation, I think my preference would be to pass a completion-handling block into the method, then call that completion handler block on the main thread to handle the results of the API call—it feels like there's a bit too much view-controller logic going on in the API method and using a completion-handling block (or a delegate callback method) would help move that logic back to the view controller.
Also, though it doesn't really change anything, you can replace the calls
dispatch_async(dispatch_get_main_queue(), ^(void){
...
});
with
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
...
}];
(It is generally preferable to use higher-level APIs, such as NSOperationQueue, over lower-level APIs, like dispatch_async, when they are equivalent.)

GCD slow updating UI

Im using GCD to download a plist file from the internet then enter the data into an array then load it into a tableView.
The data is being downloaded and converted alright but the table view is taking ages to update. I have the app Logging something when it calls [self.tableView reloadData] but the table view updates about 10 seconds later.
I don't think this is about the table view because I tried changing the title of the Navigation Bar inside the GCD and there was also a delay. So it might be a delay updating the user interface?
This is my GCD code:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
Thanks for you help
EDIT:
This is my code:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UERootArray = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:#"url to file"]];
NSLog(#"Array: %#", UERootArray);
//NSLog(#"count %#",count);
NSString *currentName;
NSString *currentLoaction;
NSString *currentDate;
int currentEventInt = 0;
NSArray *currentEventArr = [UERootArray objectAtIndex:0];
while (currentEventArr.count > currentEventInt)
{
currentName = [currentEventArr objectAtIndex:0];
currentLoaction = [currentEventArr objectAtIndex:1];
currentDate = #"7pm 17/11"; //[currentEventArr objectAtIndex:2];
NSLog (#"Title: %#", currentName);
NSLog (#"News: %#", currentLoaction);
NSLog (#"Date: %#", currentDate);
UEEvent *currentTask = [[UEEvent alloc] initWithName:currentName location:currentLoaction date:currentDate];
[self.upcommingEvents addObject:currentTask];
currentEventInt = currentEventInt + 1;
}
UEDownloaded = YES;
[self.tableView reloadData];
Edit 2:
- (void)downloadUEData
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UERootArray = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:#"url to file"]];
[self processUEData];
});
}
- (void)processUEData
{
NSLog(#"Array: %#", UERootArray);
//NSLog(#"count %#",count);
NSString *currentName;
NSString *currentLoaction;
NSString *currentDate;
int currentEventInt = 0;
NSArray *currentEventArr = [UERootArray objectAtIndex:0];
while (currentEventArr.count > currentEventInt)
{
currentName = [currentEventArr objectAtIndex:0];
currentLoaction = [currentEventArr objectAtIndex:1];
currentDate = #"7pm 17/11"; //[currentEventArr objectAtIndex:2];
NSLog (#"Title: %#", currentName);
NSLog (#"News: %#", currentLoaction);
NSLog (#"Date: %#", currentDate);
UEEvent *currentTask = [[UEEvent alloc] initWithName:currentName location:currentLoaction date:currentDate];
[self.upcommingEvents addObject:currentTask];
currentEventInt = currentEventInt + 1;
}
UEDownloaded = YES;
[self.tableView reloadData];
}
i think following code will solve your problem ...
- (void)downloadUEData
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UERootArray = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.nytrex.webs.com/Linx/upcommingEvents.plist"]];
[self processUEData];
});
}
- (void)processUEData
{
NSLog(#"Array: %#", UERootArray);
//NSLog(#"count %#",count);
NSString *currentName;
NSString *currentLoaction;
NSString *currentDate;
int currentEventInt = 0;
NSArray *currentEventArr = [UERootArray objectAtIndex:0];
while (currentEventArr.count > currentEventInt)
{
currentName = [currentEventArr objectAtIndex:0];
currentLoaction = [currentEventArr objectAtIndex:1];
currentDate = #"7pm 17/11"; //[currentEventArr objectAtIndex:2];
NSLog (#"Title: %#", currentName);
NSLog (#"News: %#", currentLoaction);
NSLog (#"Date: %#", currentDate);
UEEvent *currentTask = [[UEEvent alloc] initWithName:currentName location:currentLoaction date:currentDate];
[self.upcommingEvents addObject:currentTask];
currentEventInt = currentEventInt + 1;
}
UEDownloaded = YES;
dispatch_async(dispatch_get_main_queue(), ^{ // update your UI in main thread
[self.tableView reloadData];
});
}

dispatch_queue and return data

I'm trying to write this method that returns an NSArray. My NSMutableArray (friendUsers) adds the objects right, but outside the dispatch_async the array is empty.
I try to add the users in the main queue ( as ashowed) but the array is empty to. Any ideas ? Thanks for all your help.
- (NSArray *)checkUsersInGroup {
NSMutableArray *friendUsers = [[NSMutableArray alloc] init];
dispatch_queue_t checkUSers = dispatch_queue_create("CheckUsers", NULL);
dispatch_async(checkUSers, ^{
NSArray *totalUsers = [VVDataRead lecturaDades];
NSArray *usersToSearch = [_grup objectForKey:#"groupFriends"];
for (NSString *tempUserId in usersToSearch){
for (NSDictionary *user in totalUsers){
NSString *id = [user objectForKey:#"id"];
if ([tempUserId isEqualToString:id])
dispatch_async(dispatch_get_main_queue(), ^{
[friendUsers addObject:user];
});
}
}
});
NSLog(#"people:%#",friendUsers);
return [friendUsers copy];
}
you can use blocks, it can make your life easier in this case.
- (void)checkUsersInGroupWithCompleteBlock:(void(^)(NSMutableArray * resultArray))completeBlock {
NSMutableArray *friendUsers = [[NSMutableArray alloc] init];
dispatch_queue_t checkUSers = dispatch_queue_create("CheckUsers", NULL);
dispatch_async(checkUSers, ^{
NSArray *totalUsers = [VVDataRead lecturaDades];
NSArray *usersToSearch = [_grup objectForKey:#"groupFriends"];
for (NSString *tempUserId in usersToSearch){
for (NSDictionary *user in totalUsers){
NSString *id = [user objectForKey:#"id"];
if ([tempUserId isEqualToString:id])
dispatch_async(dispatch_get_main_queue(), ^{
[friendUsers addObject:user];
});
}
}
// call the complete block with the result when you finished
if (completeBlock) completeBlock(friendUsers);
});
}
...and here is how you can call the method:
- (void)anyMethod {
// ... do whetever you want here before
[self checkUsersInGroupWithCompleteBlock:^(NSMutableArray *resultArray) {
NSLog(#"%#", resultArray);
}];
// ... or after
}
EDITED:
NOTE: here is another possible solution, but in your case it just suspends the main thread (which is definitely bad), so you won't gain anything with this solution but pain on the main thread, but if you are on two background threads, this solution can give a very nice example of synchronisation between the threads.
- (NSArray *)checkUsersInGroup {
NSMutableArray *friendUsers = [[NSMutableArray alloc] init];
// our semaphore is here
dispatch_semaphore_t _semaphore = dispatch_semaphore_create(0);
dispatch_queue_t checkUSers = dispatch_queue_create("CheckUsers", NULL);
dispatch_async(checkUSers, ^{
NSArray *totalUsers = [VVDataRead lecturaDades];
NSArray *usersToSearch = [_grup objectForKey:#"groupFriends"];
for (NSString *tempUserId in usersToSearch){
for (NSDictionary *user in totalUsers){
NSString *id = [user objectForKey:#"id"];
if ([tempUserId isEqualToString:id])
dispatch_async(dispatch_get_main_queue(), ^{
[friendUsers addObject:user];
});
}
}
// the process finished
dispatch_semaphore_signal(_semaphore);
});
// ... we are wainitng for the semaphore's signal
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(_semaphore);
NSLog(#"people:%#",friendUsers);
return [friendUsers copy];
}
There are number of strategies to solve this, but as your operation happens on a background thread, returning the array isn't one of them. You could use NSNotificationCenter to signal that the task as finished and read the array. i.e.
- (void)checkUsersInGroup {
NSMutableArray *friendUsers = [[NSMutableArray alloc] init];
dispatch_queue_t checkUSers = dispatch_queue_create("CheckUsers", NULL);
dispatch_async(checkUSers, ^{
NSArray *totalUsers = [VVDataRead lecturaDades];
NSArray *usersToSearch = [_grup objectForKey:#"groupFriends"];
for (NSString *tempUserId in usersToSearch){
for (NSDictionary *user in totalUsers){
NSString *id = [user objectForKey:#"id"];
if ([tempUserId isEqualToString:id])
dispatch_async(dispatch_get_main_queue(), ^{
[friendUsers addObject:user];
});
}
}
// Signal background task is finished
// Make sure to add an observer to this notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"friendsAddLiteral"
object:nil];
});
}
//this method will respond to the notification
- (void) onFriendsAdded:(NSNotification*)notif {
//do something on the main thread
}

Resources