I try to use MBProgressHUD or ZAActivityBar as progress bar in my cocoa project. All fine, but in one place this progress bar view appear only after performing function from singleton class, but should during.
-(IBAction)importButtonClicked:(id)sender
{
//[ZAActivityBar showSyncWithStatus:NSLocalizedString(@"MBProgressHUDLabel", nil) forAction:@"importButtonClicked"];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
int count=0;
count=[[CoreDataHelper helperCoreDataHelper] saveImportedFriends:_importedFriendsDictionary withOnlyDate:_onlyDateSwitcher withRewrite:(int)_rewriteSwitcher];
//[ZAActivityBar dismissSyncForAction:@"importButtonClicked"];
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
CoreDataHelper.m
-(int)saveImportedFriends:(NSMutableSet*)set withOnlyDate:(int)withDate withRewrite:(int)rewrite
{
int count=0;
for(FriendsForImport* friendsForImport in set)
{
if((withDate==1 && friendsForImport.birthday.length>0) || withDate==0)
{
Friends *friendsExist=[self checkFriendAlreadyExist:friendsForImport];
if((friendsExist.lastName.length>0 || friendsExist.firstName.length>0) && rewrite==0)
continue;
else if((friendsExist.lastName.length>0 || friendsExist.firstName.length>0) && rewrite==1)
[self deleteFriendAlreadyExist:friendsExist];
UIImage *image;
if([friendsForImport.importFrom intValue]==1)
{
image = [[UIImageHelper helper] getUIImageFromAddressBookContactId:[friendsForImport.uid integerValue]];
}
else
image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:friendsForImport.photoPath]]];
NSString *imageName=@"";
if(image.size.height>0)
imageName=[[UIImageHelper helper] saveImage:[[UIImageHelper helper] resizedImage:image withRect:CGRectMake(0, 0, kAvatarSize, kAvatarSize)]];
Friends *friends= (Friends *)[NSEntityDescription insertNewObjectForEntityForName:@"Friends" inManagedObjectContext:_managedObjectContext];
friends.lastName=friendsForImport.lastName;
friends.firstName=friendsForImport.firstName;
friends.uid=[NSString stringWithFormat:@"%@",friendsForImport.uid];
friends.birthday=[[CoreDataHelper helperCoreDataHelper] properDateString:friendsForImport.birthday];
friends.alarm=[NSNumber numberWithInt:1];
friends.important=NO;
friends.photoPath=imageName;
friends.importFrom=friendsForImport.importFrom;
friends.importDate=[NSDate date];
//NSLog(@"friends %@",friends);
NSError *error = nil;
if (![_managedObjectContext save:&error]) {
NSLog(@"Error in adding a new bank %@, %@", error, [error userInfo]);
abort();
}
count++;
}
}
return count;
}
Your -saveImportedFriends:withOnlyDate:withRewrite:
method blocks the main thread and thus the progress view never becomes visible.
You should change your logic so that the method can perform its task asynchronously in the background. Because you're using the managed object context to insert and eventually save the imported friends, you can't just wrap your code inside dispatch_async()
, though.
This related answer should get you on the right track: https://stackoverflow.com/a/2141381/322548
EDIT:
You shouldn't invoke -save:
every time you add a new friend to the managed object context, btw. Only calling it once when you're done with all friends is just fine.