gpt4 book ai didi

ios - xCode cocoa : MBProgressHUD or ZAActivityBar in one place appear only after performing function, 但应该在

转载 作者:行者123 更新时间:2023-11-29 04:06:27 25 4
gpt4 key购买 nike

我尝试在我的 cocoa 项目中使用MBProgressHUDZAActivityBar作为进度条。一切都很好,但是在一个地方,这个进度条 View 仅在从单例类执行函数之后出现,但应该在执行期间出现。

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

最佳答案

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

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

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

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

关于ios - xCode cocoa : MBProgressHUD or ZAActivityBar in one place appear only after performing function, 但应该在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15117714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com