多线程iOS KVO进程的问题

时间:2016-07-12 13:43:10

标签: ios objective-c multithreading progress-bar key-value-observing

将KVO流程从macOS转移到iOS时出现问题。

在Mac程序中,用户填写一个屏幕,然后按下“go”按钮,该按钮提交后台任务并启动一个新窗口。新窗口使用KVO从后台任务获取状态消息流并将其显示在屏幕上。

在iOS程序中,我正在尝试执行类似的过程来点亮进度条以及捕获一些状态信息。该程序进入一个新的故事板面板,只有一个进度条(现在)和一个完成按钮。将出现故事板面板,并执行“viewDidAppear”方法以启动后台任务。后台任务运行但observeValueForKeyPath:方法永远不会触发。我想知道我是否在某处遇到某种线程并发症。

窗口不是弹出窗口。它被这样命名并改为常规故事板' show'类型面板。

按下原始“go”按钮时,将执行以下代码:

-(IBAction)clearCloudDB:(id)sender {

    ClearCloudDBClass* tempClearCloudDBClass = [[ClearCloudDBClass alloc] init];
    savedClearCloudDBClass = tempClearCloudDBClass;
    tempClearCloudDBClass.localFindItDataController = self.localFindItDataController;

    [self performSegueWithIdentifier: @"clearPopover" sender: self];


}

segue代码如下:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
   {
    if ([segue.identifier isEqualToString:@"backupPopover"]) {
    BackupPopover *vc = [segue destinationViewController];

    vc.modalPresentationStyle = UIModalPresentationPopover;
    vc.popoverPresentationController.delegate = nil;}

if ([segue.identifier isEqualToString:@"clearPopover"]) {
    ClearPopover *vc = [segue destinationViewController];

    vc.localSecondViewController = self;
    vc.localClearCloudDBClass = savedClearCloudDBClass;}

} 

这是后台进程类h文件:

@class ClearPopover;

@interface ClearCloudDBClass : NSObject {

NSInteger iteration_counter;
NSInteger total_counter;

FindItDataController* localFindItDataController;
ClearPopover* localClearPopover;

}

 @property NSInteger iteration_counter;
@property NSInteger total_counter;
@property FindItDataController *localFindItDataController;
@property ClearPopover *localClearPopover;


- (void) ClearCloudDBClassRun:(id) parameterTestFinditAppDelegate;

@end

这是后台进程类m文件。请原谅不完整的iCloud编码。暂时没有捕获错误:

#import "ClearCloudDBClass.h"
#import "Item.h"

@implementation ClearCloudDBClass

@synthesize iteration_counter, total_counter, localFindItDataController,     localClearPopover;

- (void) ClearCloudDBClassRun:(NSObject *) parameterTestFinditCaller {

dispatch_queue_t backgroundQueue = dispatch_queue_create("Background Queue",NULL);

dispatch_async(backgroundQueue, ^{

    NSError *error;
    NSArray *nsarrayFetchedObjects;
    Item *tempItem;
    total_counter = 0;
    iteration_counter = 0;

    NSFetchRequest *totalcountfetch = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityDescription1 = [NSEntityDescription entityForName:@"Item"  inManagedObjectContext: localFindItDataController.managedObjectContext];
    [totalcountfetch setEntity:entityDescription1];
    NSUInteger reccount =
        [localFindItDataController.managedObjectContext countForFetchRequest:totalcountfetch error:&error];
    total_counter = (int)reccount;

    NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Item"  inManagedObjectContext: localFindItDataController.managedObjectContext];
    [fetch setEntity:entityDescription];
    [fetch setFetchLimit:1];

    nsarrayFetchedObjects = [localFindItDataController.managedObjectContext executeFetchRequest:fetch error:&error];

    while ([nsarrayFetchedObjects count] != 0) {
        tempItem = [nsarrayFetchedObjects objectAtIndex:0];
        [localFindItDataController.managedObjectContext deleteObject:tempItem];
        nsarrayFetchedObjects = [localFindItDataController.managedObjectContext executeFetchRequest:fetch error:&error];
        [localFindItDataController.managedObjectContext save:&error];
        iteration_counter++;}

    iteration_counter = total_counter;

});

}

最后,这是视图控制器中的KVO代码。

-(void)viewDidAppear:(BOOL)animated {

[localClearCloudDBClass addObserver:self
                         forKeyPath:@"iteration_counter"
                            options:NSKeyValueObservingOptionNew
                            context:NULL];

[localClearCloudDBClass addObserver:self
                         forKeyPath:@"total_counter"
                            options:NSKeyValueObservingOptionNew
                            context:NULL];

progressBar.progress = 0.00;

[localClearCloudDBClass ClearCloudDBClassRun:self];

}

这个过程可能会有一些错误,但我根本无法解决它:

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change
context:(void *)context
{

[[NSOperationQueue mainQueue] addOperationWithBlock:^{

    NSLog(@"keyPath %@", keyPath);

    int iteration_counter;
    int total_counter;

    iteration_counter = (int) localClearCloudDBClass.iteration_counter;
    total_counter = (int) localClearCloudDBClass.total_counter;

    if (total_counter != 0) {
        progressBar.progress = (float) iteration_counter / (float) total_counter;}
    else {
        progressBar.progress = 100;}

 }];

}

思想?

0 个答案:

没有答案