关于ios6.0 日历写入 block的问题

linqir 发布于 2013/02/28 14:05
阅读 338
收藏 0
iOS

@Railgun 你好,想跟你请教个问题:

以下是我的代码,现在遇到的问题是,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]; } } (2013/02/28 15:13)
加载中
0
Railgun
Railgun

你现在的代码肯定是这样的:

if ([self IncreaseCelendarInfo:array]) {
    //代码
}
而你要做的是,把“//代码”这一部分放到 requestAccessToEntityType: completion:的block里,这样才能确保在block执行完成之后才去执行后面的语句。

sleep(2)之后就有返回是因为刚好2秒只内回调到了block里的语句,这不能保证每次都能得到正确的isSuccess值。比如,requestAccessToEntityType: completion:执行了3秒,这个很难说的。

requestAccessToEntityType: completion:是异步的,也就是说,程序执行完这句之后不会等待,而是立即继续执行下面的return或者sleep,所以,你需要用处理异步回调的方法来继续执行。-(BOOL)IncreaseCelendarInfo:(NSArray *)celendarArray 这样立即返回是不行的。requestAccessToEntityType: completion:的completion block就是一个现成的回调。你所有需要依赖requestAccessToEntityType: completion:返回结果的逻辑和代码都要放在这里面。

返回顶部
顶部