0

我尝试在我的可可项目中使用MBProgressHUDZAActivityBar作为进度条。一切都很好,但在一个地方,这个进度条视图仅在单例类执行功能后出现,但应该在此期间出现。

-(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;
}
4

1 回答 1

1

您的-saveImportedFriends:withOnlyDate:withRewrite:方法阻塞了主线程,因此进度视图永远不会变得可见。

您应该更改您的逻辑,以便该方法可以在后台异步执行其任务。因为您使用托管对象上下文来插入并最终保存导入的朋友,所以您不能只将代码包装在dispatch_async().

这个相关的答案应该让你走上正轨:https ://stackoverflow.com/a/2141381/322548

编辑:
您不应该在-save:每次将新朋友添加到托管对象上下文时调用,顺便说一句。只在与所有朋友结束时调用一次就可以了。

于 2013-02-27T17:01:25.613 回答