gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:44:54 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com