NS管理上下文线程

编程入门 行业动态 更新时间:2024-10-09 08:26:16
本文介绍了NS管理上下文线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用单例来处理数组等,跨越应用程序中的视图。

I use a singleton for working with arrays etc. cross the views in the application.

要初始化单例和 NSManagedObjectContext ,以便我可以获取对象,我使用:

To initialize the singleton and the NSManagedObjectContext, so that I can fetch objects, I use:

+(DataControllerSingleton *)singleDataController { static DataControllerSingleton * single=nil; @synchronized(self) { if(!single) { single = [[DataControllerSingleton alloc] init]; NSManagedObjectContext *context = [single.fetchedResultsController managedObjectContext]; single.masterCareList = [[NSMutableArray alloc] init]; } } return single; }

当我插入一个新对象时,对象不会显示在显示函数中,直到我重新启动应用程序。我通过这个方法在单例类中插入新对象:

When I insert a new object that object will not show up in display functions until I restart the application. I insert new object through this method in the singleton class:

- (void)insertNewObject:(Care *)care { NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext]; NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity]; NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName: [entity name] inManagedObjectContext:self.managedObjectContext]; NSString *fileName = care.pictureURL; NSString *text = care.causeText; NSDate *date = care.date; NSData *imgData = care.imageData; [newManagedObject setValue:fileName forKey:@"urlPath"]; [newManagedObject setValue:text forKey:@"name"]; [newManagedObject setValue:date forKey:@"date"]; [newManagedObject setValue:imgData forKey:@"imageData"]; // Save the context. [self saveContext]; NSError *error = nil; if (![context save:&error]) { // Replace this implementation with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } }

我的计数方法是我可以告诉新对象不包括在内,直到我重新启动应用程序。 count方法也在singleton中。

My count method is the way I can tell that the new object is not included until I restart the application. The count method is also in the singleton as well.

- (NSUInteger)countOfList { NSArray *fetchedData = [_fetchedResultsController fetchedObjects]; return [fetchedData count]; }

当调用singleton时,我使用:

When calling singleton I use:

DataControllerSingleton *singletonData = [DataControllerSingleton singleDataController]; [singletonData insertNewObject:care];

managedObjectContext 属性:

.h:

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

.m:

@implementation DataControllerSingleton @synthesize managedObjectContext = _managedObjectContext; @synthesize managedObjectModel = _managedObjectModel; @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

为什么在重新启动应用程序之前我的新对象不会显示

Why will not my new object show up in ex count until I restart application?

我不知道如何使用不同的线程与不同的上下文,或不同的fethedResultsController或不同的单例(不应该是可能的?)

Am I somehow using different threads with different contexts, or different fethedResultsController or different singleton (shouldnt be possible right?)??

我添加了这两行,它们不包括在生成的CoreData堆栈中,现在可以正常工作。

I added these two lines, which are not included in the genereated CoreData Stack, and it now works fine.

在单例标题中:

@interface DataControllerSingleton : NSObject <NSFetchedResultsControllerDelegate>

在实施文件中,

(NSFetchedResultsController *)fetchedResultsController { _fetchedResultsController.delegate = self;

推荐答案

表格等。

如果您想在保存上下文时更新表格,您需要:

If you want to update the table as soon as you save the context you need to:

  • 重新载入数据表 [table reloadData];
  • (请参阅如何使用NSFetchedResultsController )
  • Reload the data table [table reloadData];
  • or implement in the correct delegate methods of (take a look to How To Use NSFetchedResultsController)
  • 如果您遵循第一个选项,您只需在上下文中执行保存并调用表上的realod数据。

    If you follow the first option, you can just do a save in the context and call realod data on the table.

    NSError *error = nil; if (![context save:&error]) { // Replace this implementation with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should //not use this function in a shipping application, although it may be useful during development. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } [table reloadData];

    请注意,您正在致电保存两次。在这种情况下,我假设 [self saveContext]; 执行如上保存。

    NOTE THAT YOU ARE CALLING THE SAVE TWICE Do it once. In this case I suppose that [self saveContext]; does the saving as above.

    希望有帮助。

    编辑

    您获取的结果控制器的委托应该是一个视图控制器(包含表的那个)。不要把它放在你的单身!

    The delegate of your fetched results controller should be a view controller (the one that contains the table). Do not put it in your singleton!!

    更多推荐

    NS管理上下文线程

    本文发布于:2023-11-09 08:00:15,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1571810.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:上下文   线程   NS

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!