Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 进行奇怪的行为_Ios_Objective C_Core Data_Nsprogress - Fatal编程技术网

Ios 进行奇怪的行为

Ios 进行奇怪的行为,ios,objective-c,core-data,nsprogress,Ios,Objective C,Core Data,Nsprogress,我有一个由几个子任务组成的大任务。我想为这项重大任务添加进度报告。 为此,我想使用NSProgress,根据类文档,我可以使用其子-父机制来完成这种子任务进度 为了简化它,假设我有一个大任务,它由一个子任务组成(当然在现实生活中会有更多的子任务)。这就是我所做的: #define kFractionCompletedKeyPath @"fractionCompleted" - (void)runBigTask { _progress = [NSProgress progressWi

我有一个由几个子任务组成的大任务。我想为这项重大任务添加进度报告。
为此,我想使用
NSProgress
,根据类文档,我可以使用其子-父机制来完成这种子任务进度

为了简化它,假设我有一个大任务,它由一个子任务组成(当然在现实生活中会有更多的子任务)。这就是我所做的:

#define kFractionCompletedKeyPath @"fractionCompleted"  

- (void)runBigTask {
    _progress = [NSProgress progressWithTotalUnitCount:100]; // 100 is arbitrary 

    [_progress addObserver:self
                forKeyPath:kFractionCompletedKeyPath
                   options:NSKeyValueObservingOptionNew
                   context:NULL];

    [_progress becomeCurrentWithPendingUnitCount:100]; 
    [self subTask];
    [_progress resignCurrent];
} 

- (void)subTask {
    NSManagedObjectContext *parentContext = self.managedObjectContext; // self is AppDelegate in this example
    NSManagedObjectContext *bgContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [bgContext setParentContext:parentContext];

    [bgContext performBlockAndWait:^{
        NSInteger totalUnit = 1000;
        NSInteger completedUnits = 0;
        NSProgress *subProgress = [NSProgress progressWithTotalUnitCount:totalUnit];

        for (int i=0; i < totalUnit; i++) {   

            // run some Core Data related code...  

            completedUnits++;
            subProgress.completedUnitCount = completedUnits;
        }
    }];
}      

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:kFractionCompletedKeyPath]) {
        if ([object isKindOfClass:[NSProgress class]]) {
            NSProgress *progress = (NSProgress *)object;
            NSLog(@"progress… %f", progress.fractionCompleted);
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
如你所见,打印从1.0、0.5和1.0开始,然后是0.66
从这里开始,它表现正常,并像我预期的那样变为1.0

我试图理解为什么会发生这种情况,我注意到如果我从背景上下文中删除父上下文,它就可以正常工作!我从0.0进步到1.0

你知道为什么会这样吗?我怎样才能解决这个问题


我添加了一个非常来演示这个问题(您可以删除setParentContext:call以查看它在没有它的情况下是否工作正常)

似乎在
[NSManagedObjectModel initWithContentsOfURL:
的内部必须有一个NSProgress计数器。在进入
[self subTask]
之前,您将自己设置为接收任何进度指示器的通知(通过将
\u progress
设置为当前并注册self以观察更改)。然后在该例程中调用lazy getter
self.managedObjectContext
,它反过来调用
[NSManagedObjectModel initWithContentsOfURL:
,它显然有一个2个单位的进度计数器。在调用
[NSProgress becomeCurrentWithPendingUnitCount://code>和
[NSProgress resignCurrent]
时,似乎需要非常小心。发生这种情况时,堆栈跟踪如下所示:

(lldb) bt
* thread #1: tid = 0x81f2, 0x0000000105bffcda Foundation`-[NSProgress setTotalUnitCount:], queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x0000000105bffcda Foundation`-[NSProgress setTotalUnitCount:]
    frame #1: 0x0000000105bfeb1b Foundation`+[NSProgress progressWithTotalUnitCount:] + 87
    frame #2: 0x0000000105a31213 Foundation`_NSReadBytesFromFileWithExtendedAttributes + 287
    frame #3: 0x0000000105a3109d Foundation`-[NSData(NSData) initWithContentsOfFile:] + 89
    frame #4: 0x0000000105a30b40 Foundation`+[NSDictionary(NSDictionary) newWithContentsOf:immutable:] + 101
    frame #5: 0x0000000105a5622a Foundation`+[NSDictionary(NSDictionary) dictionaryWithContentsOfFile:] + 45
    frame #6: 0x00000001043c4560 CoreData`-[NSManagedObjectModelBundle initWithPath:] + 224
    frame #7: 0x00000001043c42ed CoreData`-[NSManagedObjectModel initWithContentsOfURL:] + 205
    frame #8: 0x00000001040f723f CDProgress`-[AppDelegate managedObjectModel](self=0x00007fbe48c21f90, _cmd=0x000000010459b37b) + 223 at AppDelegate.m:127
    frame #9: 0x00000001040f7384 CDProgress`-[AppDelegate persistentStoreCoordinator](self=0x00007fbe48c21f90, _cmd=0x000000010459c1cb) + 228 at AppDelegate.m:142
    frame #10: 0x00000001040f708c CDProgress`-[AppDelegate managedObjectContext](self=0x00007fbe48c21f90, _cmd=0x0000000104598f0d) + 92 at AppDelegate.m:111
    frame #11: 0x00000001040f6bdb CDProgress`-[AppDelegate subTask](self=0x00007fbe48c21f90, _cmd=0x00000001040f7997) + 43 at AppDelegate.m:45
    frame #12: 0x00000001040f6b89 CDProgress`-[AppDelegate runTask](self=0x00007fbe48c21f90, _cmd=0x00000001040f7928) + 233 at AppDelegate.m:40
    frame #13: 0x00000001040f6a4b CDProgress`-[AppDelegate application:didFinishLaunchingWithOptions:](self=0x00007fbe48c21f90, _cmd=0x0000000104f5dba9, application=0x00007fbe48f00fb0, launchOptions=0x0000000000000000) + 571 at AppDelegate.m:26
    frame #14: 0x000000010477c5a5 UIKit`-[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 234
    frame #15: 0x000000010477d0ec UIKit`-[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2463
    frame #16: 0x000000010477fe5c UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
    frame #17: 0x000000010477ed22 UIKit`-[UIApplication workspaceDidEndTransaction:] + 179
    frame #18: 0x00000001088092a3 FrontBoardServices`__31-[FBSSerialQueue performAsync:]_block_invoke + 16
    frame #19: 0x000000010615fabc CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    frame #20: 0x0000000106155805 CoreFoundation`__CFRunLoopDoBlocks + 341
    frame #21: 0x00000001061555c5 CoreFoundation`__CFRunLoopRun + 2389
    frame #22: 0x0000000106154a06 CoreFoundation`CFRunLoopRunSpecific + 470
    frame #23: 0x000000010477e799 UIKit`-[UIApplication _run] + 413
    frame #24: 0x0000000104781550 UIKit`UIApplicationMain + 1282
    frame #25: 0x00000001040f7793 CDProgress`main(argc=1, argv=0x00007fff5bb09308) + 115 at main.m:16
    frame #26: 0x000000010686f145 libdyld.dylib`start + 1
(lldb) 
这里发生的事情是,当加载模型时,它正在读取plist文件。读取plist文件将调用
-[NSData initWithContentsOfFile:
,它正在主线程上调用
+[nsprogresswithtotalunitcount:
。作为,这将创建一个NSProgress,它是当前进度的子进程
initWithContentsOfFile:
实际上正在执行此操作,并创建您创建的
NSProgress
的新子级:

<NSProgress: 0x7f9353596f80> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of 1  
   <_NSProgressGroup: 0x7f935601a0d0> : Portion of parent: 100 Children: 1
      <NSProgress: 0x7f935600bf50> : Parent: 0x7f9353596f80 / Fraction completed: 0.0000 / Completed: 0 of 0 

NSProgress可能有点难,但有了一些经验,它会变得更容易。我保证

NSProgress的最佳文档在这里:作为旁注,我将把它作为bug归档。最坏的情况是,他们告诉你它按预期工作。
<NSProgress: 0x7f9353596f80> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of 1  
   <_NSProgressGroup: 0x7f935601a0d0> : Portion of parent: 100 Children: 1
      <NSProgress: 0x7f935600bf50> : Parent: 0x7f9353596f80 / Fraction completed: 0.0000 / Completed: 0 of 0 
- (void)subTask {
    NSProgress  *progress   = [NSProgress progressWithTotalUnitCount:1];
    NSManagedObjectContext *parentContext = self.managedObjectContext;
    NSManagedObjectContext *bgContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [bgContext setParentContext:parentContext];

    [progress becomeCurrentWithPendingUnitCount:1];
    [bgContext performBlock:^{

    ... stuff

    [progress resignCurrent];
}