开发者社区> 问答> 正文

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

你好,想跟你请教个问题: 以下是我的代码,现在遇到的问题是,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年前)

展开
收起
杨冬芳 2016-07-01 13:12:36 2154 0
1 条回答
写回答
取消 提交回答
  • IT从业

    你现在的代码肯定是这样的: ?123if ([self IncreaseCelendarInfo:array]) { //代码}而你要做的是,把“//代码”这一部分放到 requestAccessToEntityType: completion:的block里,这样才能确保在block执行完成之后才去执行后面的语句。 sleep(2)之后就有返回是因为刚好2秒只内回调到了block里的语句,这不能保证每次都能得到正确的isSuccess值。比如,requestAccessToEntityType: com

    2019-07-17 19:49:57
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载