首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >问题是正在运行并停止CFRunLoop

问题是正在运行并停止CFRunLoop
EN

Stack Overflow用户
提问于 2013-08-16 15:07:21
回答 1查看 711关注 0票数 0

我有这样的情况,我需要等待,直到一个块完成,然后只继续我的代码,因为我使用CFRunLooprun并停止这是如何做的,我将在我的代码注释中解释更多事情

代码语言:javascript
复制
 [self fatchAllEvent];  // BLOCK IS IN THIS METHOD 

    NSLog(@"loop will start");
    CFRunLoopRun();


    NSLog(@"LOOP IS STOOPED");

-(void)fatchAllEvent{


     events = [[NSMutableArray alloc]init];


//    // Get the appropriate calendar
    NSCalendar *calendar = [NSCalendar currentCalendar];


    eventStore = [[EKEventStore alloc] init];

    if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {
//        __block typeof (self) weakSelf = self; // replace __block with __weak if you are using ARC
        dispatch_async(dispatch_get_main_queue(), ^{
                  [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
         {

             if (granted)
             {

                 [events removeAllObjects];
                 NSLog(@" granted");
                 NSLog(@"User has granted permission!");
                 // Create the start date components
                 NSDateComponents *twoYearAgoComponents = [[NSDateComponents alloc] init];
                 twoYearAgoComponents.year = -2;
                 NSDate *oneDayAgo = [calendar dateByAddingComponents:twoYearAgoComponents
                                                               toDate:[NSDate date]
                                                              options:0];

                 // Create the end date components
                 NSDateComponents *twoYearFromNowComponents = [[NSDateComponents alloc] init];
                 twoYearFromNowComponents.year = 2;
                 NSDate *oneYearFromNow = [calendar dateByAddingComponents:twoYearFromNowComponents
                                                                    toDate:[NSDate date]
                                                                   options:0];

                 // Create the predicate from the event store's instance method
                 NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:oneDayAgo
                                                                              endDate:oneYearFromNow
                                                                            calendars:nil];

                 // Fetch all events that match the predicate
                 events =(NSMutableArray*) [eventStore eventsMatchingPredicate:predicate];
                 NSLog(@"The content of array is%@",events);



             }
             else
             {
                 NSLog(@"Not granted");



             }
                NSLog(@"LOOP WILL STOP");  // THIS GETS PRINT 
                CFRunLoopStop(CFRunLoopGetCurrent());   // BUT LOOP IS NOT STOPPING HERE SO MY APP JUST GET HANGED ;  
         }];

               });
    }
    else
    {

         [events removeAllObjects];

        NSLog(@"Autometiclly granted permission!");
        // Create the start date components
        NSDateComponents *twoYearAgoComponents = [[NSDateComponents alloc] init];
        twoYearAgoComponents.year = -2;
        NSDate *oneDayAgo = [calendar dateByAddingComponents:twoYearAgoComponents
                                                      toDate:[NSDate date]
                                                     options:0];

        // Create the end date components
        NSDateComponents *twoYearFromNowComponents = [[NSDateComponents alloc] init];
        twoYearFromNowComponents.year = 2;
        NSDate *oneYearFromNow = [calendar dateByAddingComponents:twoYearFromNowComponents
                                                           toDate:[NSDate date]
                                                          options:0];

        // Create the predicate from the event store's instance method
        NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:oneDayAgo
                                                                     endDate:oneYearFromNow
                                                                   calendars:nil];

        // Fetch all events that match the predicate
        events =(NSMutableArray*) [eventStore eventsMatchingPredicate:predicate];
        NSLog(@"The content of array is%@",events);




    }


}
EN

回答 1

Stack Overflow用户

发布于 2013-08-16 17:14:43

你不能这样做-你必须调用你的函数,然后在异步调度的完成块中,在最后调用一个函数,继续你想要做的事情。

在上面的代码中,您将异步编程与同步执行混为一谈,这是行不通的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18267684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档