ios - dispatch_async永远不会完成整个 block 的执行

标签 ios objective-c core-data grand-central-dispatch dispatch-async

我是 GCD 新手,看似简单的使用方法对我来说并不适用。我有以下代码:

+ (void)synchronizationTimerFired:(NSTimer *)theTimer
{
    if ((synchronizationUpNeededFlag) || (synchronizationDownNeededFlag))
    {
        if ((!synchronizationUpInProgressDepthQuantity) && (!synchronizationDownInProgressDepthQuantity))
        {
            dispatch_queue_t synchronizationQueue = dispatch_queue_create("synchronizationQueue",NULL);
            dispatch_async(synchronizationQueue, ^(void) {

                NSLog(@"Top");
                ...code...
                ...code...
                ...code...
                NSLog(@"Bottom");

            });
        }
    }

    // Check if there is no timer, or if it is not currently valid,
    // and yet if synchronization is turned on,
    // then establish a repeating timer to attend to synchronization related matters.
    if ((!synchronizationTimer) || (!synchronizationTimer.isValid))
    {
        if (synchronizationOnFlag)
        {
            synchronizationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(synchronizationTimerFired:) userInfo:nil repeats:YES];
        }
    }
}

日志显示“Top”,没有其他内容。中间的代码没有无限循环——它只是永远不会执行到底。我可以在代码中间放置断点,程序执行到某个点就会中断,之后就不会中断。中间有一个点,有时执行会在断点处停止,有时则不会。

在我看来,synchronizationQueue 调度队列正在被释放,但我无法调用dispatch_retain,因为编译器提示dispatch_retain 不能在ARC 中使用。我错过了什么?

为了响应人们询问中间的代码,程序执行在此方法调用中停止(由其中一行...代码...表示),位于 if (fetchArray.count) 行处,在下面评论。

+ (NSDate *)latestParseReceivedDownUpdatedAtDateForCoreDataEntityNameString:(NSString *)coreDataEntityNameString
{
    NSDate *functionReturnValue = nil;

    // Create fetchRequest
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:coreDataEntityNameString];

    // Set sort descriptor
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"parseReceivedDownUpdatedAtDate" ascending:NO]]];

    // We are only interested in one result
    [fetchRequest setFetchLimit:1];

    // Execute fetchRequest
    NSError *fetchError = nil;
    NSArray *fetchArray = [JBSAPPDELEGATE.managedObjectContext executeFetchRequest:fetchRequest error:&fetchError];

    if (fetchArray == nil)
    {
        NSLog(@"Unresolved error %@, %@", fetchError, [fetchError userInfo]);
        abort();
    }

    // If there are any records at all in our persistent store, we'll have exactly one.
    // But that doesn't mean it won't be nil, as if that record has never come down from
    // parse it will be a nil date on the managed object.
    if (fetchArray.count) // PROGRAM EXECUTION STOPS EITHER HERE, OR JUST BEFORE HERE
    {
        NSManagedObject *managedObject = [fetchArray objectAtIndex:0];
        functionReturnValue = [managedObject valueForKey:@"parseReceivedDownUpdatedAtDate"];
    }

    return functionReturnValue;
}

我要补充一点,如果我简单地注释掉对dispatch_async的调用,一切都会正常执行。它只在主线程上执行,我宁愿不这样做。

最佳答案

你的 ManagedObjectContext 是 NSManagedObjectContext 吗?如果是这样,您是否在用于调度队列的特定线程上创建了托管对象上下文?如果您阅读 NSManagedObjectContext 上的文档,它会说:

...a context assumes the default owner is the thread or queue that allocated it—this is determined by the thread that calls its init method. You should not, therefore, initialize a context on one thread then pass it to a different thread. Instead, you should pass a reference to a persistent store coordinator and have the receiving thread/queue create a new context derived from that.

关于ios - dispatch_async永远不会完成整个 block 的执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19747960/

相关文章:

ios - 在 Core-Data 中使用谓词或关系哪个更好?

ios - 对解析本地数据存储和缓存感到困惑

objective-c - 测试核心数据应用

ios - 移动 UITextField 的左 View

ios - 如何从字符串列表中找到特定的字符串?

objective-c - 继承自 NSManagedObject 的单元测试模型类

ios - 无法找到 respondsToSelector :@selector(charValue) in Swift 3 的等价物

ios - 将 UIView 输出到带有附加 View "inserted"的 UIImage 的最佳方法

ios - WatchKit:如何将GLANCE INTERFACE带到 watch 显示屏上?

ios - AVAudioSession 密码锁在 iOS8 上停止 AVAudioRecorder