你好,想跟你请教个问题: 以下是我的代码,现在遇到的问题是,issuccess返回的值一直都是初始化的值,就是在block块代码执行之前就放回类,但是如果在ruturn前加个sleep(2),就会在执行完block里的代码后ruturn,请问下如何能够让程序执行万for里面的遍历后在执行到ruturn?
//添加本地日程备份
-(BOOL)IncreaseCelendarInfo:(NSArray *)celendarArray
{
__block BOOL isSuccess = NO;
[self removeLocalCelendarInfo];
for (NSDictionary *celendar in celendarArray) {
EKEventStore *eventDB = [[EKEventStore alloc] init];
[eventDB requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
NSLog(@"granted = %d", granted);
if (granted == YES) {
EKEvent *myEvent = [EKEvent eventWithEventStore:eventDB];
NSDate *StartDate = [NSDate dateWithTimeIntervalSince1970:[[[celendar objectForKey:@"btime"] substringToIndex:10] longLongValue]];
// StartDate = [formatter dateFromString:[[celendar objectForKey:@"btime"] substringToIndex:10]];
NSDate *EndDate = [NSDate dateWithTimeIntervalSince1970:[[[celendar objectForKey:@"etime"] substringToIndex:10] longLongValue]];
// NSLog(@"date1:%@",date);
NSLog(@"startdate = %@, enddate = %@", StartDate, EndDate);
NSLog(@"dd = %@", [[celendar objectForKey:@"btime"] substringToIndex:10]);
myEvent.title = [celendar objectForKey:@"title"];
myEvent.startDate = StartDate;
myEvent.endDate = EndDate;
myEvent.allDay = [[celendar objectForKey:@"allday"] integerValue];
myEvent.location = [celendar objectForKey:@"addr"];
myEvent.notes = [celendar objectForKey:@"explain"];
[myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];
NSError *err;
isSuccess = [eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err];
NSLog(@"isSuccess = %d", isSuccess);
}
}];
[eventDB release];
}
// sleep(2);
NSLog(@"2isSuccess = %d", isSuccess);
return isSuccess;
}
---------------问题补充---------------
@linqir:调用的时候是在另一个类里面调用的 if ([celOperate IncreaseCelendarInfo:celendarArray]) { if (celendarHttpDelegate && [celendarHttpDelegate respondsToSelector:@selector(YMCelendarHttpProgress:)]) { [celendarHttpDelegate performSelector:@selector(YMCelendarHttpProgress:) withObject:CEL_DOWNLOAD_SUCCESS]; } } (3年前)
你现在的代码肯定是这样的: ?123if ([self IncreaseCelendarInfo:array]) { //代码}而你要做的是,把“//代码”这一部分放到 requestAccessToEntityType: completion:的block里,这样才能确保在block执行完成之后才去执行后面的语句。 sleep(2)之后就有返回是因为刚好2秒只内回调到了block里的语句,这不能保证每次都能得到正确的isSuccess值。比如,requestAccessToEntityType: com
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。