Reputation: 1741
What i am doing is applying push view controller to move to next view from table view's didSelectMethod. And on next view controller data is fetched for that view. So, the problem is the view switches only when the data is fetched completely, and it contains images and text both. But i have already applied the lazy loading for images but the issue is to move to next view immediately and then fetch data and update the UI and tableview. Below is code i am trying.
On next view controller's didLoad method.
[NSThread detachNewThreadSelector:@selector(setImage) toTarget:self withObject:nil];
the method setImage fetching all data images and text.
-(void)setImage
{
if([type isEqualToString:@"Organisation"])
{
self.mGetDataDict = [MyEventApi members:self.mUserIdDict];
self.mRecievedDataDict = [self.mGetDataDict valueForKey:@"members"];
}
if([type isEqualToString:@"Individual"]){
self.mGetDataDict = [MyEventApi friends:self.mUserIdDict];
self.mRecievedDataDict = [self.mGetDataDict valueForKey:@"friends"];
}
if([self.mGetDataDict valueForKey:@"friends"] == [NSNull null])
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"alert" message:@"You have not added any friend yet." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
else
{
self.mFrndNameArr = [[NSMutableArray alloc]init];
self.mFrndImgArr = [[NSMutableArray alloc]init];
self.mFirstNameArr = [[NSMutableArray alloc]init];
self.mLastNameArr = [[NSMutableArray alloc]init];
self.mFrndIdArr = [[NSMutableArray alloc]init];
self.mFrndMSinceArr = [[NSMutableArray alloc]init];
self.mFrndDescArr = [[NSMutableArray alloc]init];
self.mFrndNameArr = [self.mRecievedDataDict valueForKey:@"username"];
self.mFrndImgArr = [self.mRecievedDataDict valueForKey:@"image"];
self.mFirstNameArr = [self.mRecievedDataDict valueForKey:@"firstName"];
self.mLastNameArr = [self.mRecievedDataDict valueForKey:@"lastName"];
self.mFrndIdArr = [self.mRecievedDataDict valueForKey:@"id"];
self.mFrndMSinceArr = [self.mRecievedDataDict valueForKey:@"memberSince"];
self.mFrndDescArr = [self.mRecievedDataDict valueForKey:@"description"];
[self.mFriendsTable reloadData];
}
}
Please guide for above, is i am using correct method or there is another way to do this. Thanks in advance.
Upvotes: 0
Views: 361
Reputation: 1260
I would recommend against using the NSThread API in your context. API like GCD or NSOperation are sitting at a higher level and will provide you with the solution you are looking for:
An implementation with GCD will look like this
dispatch_queue_t backgroundQueue = dispatch_queue_create("backgroundQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(backgroundQueue, ^{
// update your method to remove the table refresh
[self setImage];
dispatch_async(dispatch_get_main_queue(), ^{
//Update the UI on the main thread
[self.mFriendsTable reloadData];
});
Upvotes: 0
Reputation: 108
Please use GCD :)
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
//in this scope load your data from server
dispatch_async(dispatch_get_main_queue(), ^{
//use this scope to reload UI with data received from scope above
// in your case this is NSDictionary object, this object will be able to use in this scope because this scope will catch variables from scope above
});
});
Upvotes: 1
Reputation: 12641
you can use GCD
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
//Fire api
dispatch_async(dispatch_get_main_queue(), ^{
//update ui
});
});
Upvotes: 0