簡體   English   中英

使用GCD從核心數據中獲取數據會導致IOS問題

[英]Fetching data from core data using GCD causes issue IOS

嗨,我正在開發IOS應用程序,我正在使用核心數據。 在我的應用程序中,我使用拆分視圖控制器。 所以我的拆分視圖控制器的主視圖包含以下內容:

-(void) viewWillAppear:(BOOL)animated
{
[self getNotificationCounter];
    [self getTicketCounter];
}
-(void) getNotificationCounter:(UITableViewCell *) cell
{
dispatch_queue_t myQueue = dispatch_queue_create("My Notification Queue",NULL);
dispatch_async(myQueue, ^{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"NotificationsData" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSError *error = nil;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];        dispatch_async(dispatch_get_main_queue(), ^{
       //update view;
    });
});
}

-(void) getTicketCounter:(UITableViewCell *) cell
{
dispatch_queue_t myQueue = dispatch_queue_create("My Ticket Queue",NULL);
dispatch_async(myQueue, ^{

    NSFetchRequest *ticketFetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *ticketEntity = [NSEntityDescription
                                         entityForName:@"Tickets" inManagedObjectContext:context];
    [ticketFetchRequest setEntity:ticketEntity];
    NSError *ticketError = nil;
    NSArray *ticketFetchedObjects = [context executeFetchRequest:ticketFetchRequest error:&ticketError];
       dispatch_async(dispatch_get_main_queue(), ^{
        // update view
       });
});
}

和細節視圖控制器有以下的事情。

- (NSFetchedResultsController *)fetchedResultsController {

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@“channels” inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                          initWithKey:@"channelId" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:context sectionNameKeyPath:nil
                                               cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;

return _fetchedResultsController;
}

所以我的問題是這樣的:在我的詳細視圖控制器中我有一個NSFetchRequestController,在主視圖中有兩個NSFetchRequest和兩個GCD。 如果我以縱向模式啟動我的應用程序,它的工作正常。 但在橫向模式下,它會停止而不會出現任何錯誤。 另一件事如果從主控制器中刪除一個GCD NSFetchRequest它工作正常或如果我刪除NSFetechResultController然后它也可以正常工作。 為什么會這樣? 難道我做錯了什么。 需要一些幫助。 謝謝。

您可能不允許NSManagedObjectContextNSManagedObject交叉線程。

您必須為線程創建一個新的上下文。

dispatch_queue_t myQueue = dispatch_queue_create("My Ticket Queue",NULL);
dispatch_async(myQueue, ^{

NSManagedObjectContext *threadContext = [[NSManagedObjectContext alloc] init];
threadContext.parent = context; //assumes other context is NSMainThreadConcurrencyType

    NSFetchRequest *ticketFetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *ticketEntity = [NSEntityDescription
                                         entityForName:@"Tickets" inManagedObjectContext:threadContext];
    [ticketFetchRequest setEntity:ticketEntity];
... 

要在上下文之間獲取NSManagedObject ,請使用它的objectID屬性和[NSManagedObjectContext objectWithId:]

如果您進行編輯,則需要保存線程上下文。 這些更改將自動傳播到父上下文。

否則,您會像一張紙一樣丟棄塊末尾的上下文。

編輯

此外,如果對象的計數是你想要的唯一的東西,那么你可以使用countForFetchRequest:error: on NSManagedObjectContext

這將是一個更便宜的調用,因為沒有為您創建對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM