Tag: 核心数据

iOS核心数据错误 – 对于多对多关系,不可接受的值types

我正在把这个问题扯出来,这似乎应该是一件非常简单的事情。 iOS / Objective C对我来说是新的,所以也许我只是没有抓住一些根本的东西。 问题是我已经添加了一个新的实体到我的核心数据模型,并build立了一个一对多的关系。 该模型已经有两个具有一对一关系的实体。 玩家实体是新的。 我有一个UITableViewController我保存到UITextFields的属性。 这对Team / TeamDetails的原始configuration工作正常,但是当我将Players实体添加到代码时,它会崩溃并出现以下错误: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-many relationship: property = "playerDetails"; desired type = NSSet; given type = Players; 所以,在我的Save方法中,我有: Teams *team = [NSEntityDescription insertNewObjectForEntityForName:@"Teams" inManagedObjectContext:self.managedObjectContext]; TeamDetails *teamdetails = [NSEntityDescription insertNewObjectForEntityForName:@"TeamDetails" inManagedObjectContext:self.managedObjectContext]; team.teamDetails = teamdetails; 然后我继续设置属性,如: […]

MagicalRecord:如何在后台保存导入的数据

我试图导入数据到核心数据,并保存在一个后台线程与MagicalRecord 。 我基本上是这样做的: __block User *user = nil; [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) { user = [User MR_findFirstOrCreateByAttribute:@"userId" withValue:userId inContext:localContext]; [user MR_importValuesForKeysWithObject:responseObject]; }]; [User setCurrentUser:user]; 当我在该块内时,用户是正确的。 块完成后, user是NSManagedObject对象,但没有设置任何属性。 另一方面,这是工作的: OEUser *user = [OEUser MR_findFirstOrCreateByAttribute:@"userId" withValue:userId]; [user MR_importValuesForKeysWithObject:responseObject]; [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait]; [User setCurrentUser:user]; 但是我想把它保存在后台。 我也试过这个无济于事。 User *user = [User MR_findFirstOrCreateByAttribute:@"userId" withValue:userId]; [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) { User *localUser = […]

从Core Data,iOS获取上次插入的项目

有没有办法让核心数据库中的最后一个插入的项目?

在核心数据中使用多个上下文时出错133000

我花了好几天的时间来尝试解决这个问题的所有可能的解决scheme,但似乎没有任何解决方法。 我运行一个像这样的后台线程: [MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext) { Media *localMedia = [media inContext:localContext]; UIImage *image = localMedia.thumbnail; dispatch_async(dispatch_get_main_queue(), ^{ [thumbnails setObject:image forKey:[NSNumber numberWithInt:i]]; [contentDict setObject:thumbnails forKey:@"MediaItems"]; [cell.entryView setNeedsDisplay]; }); }]; 或者像这样: dispatch_queue_t cellSetupQueue = dispatch_queue_create("com.Journalized.SetupTimelineThumbnails", NULL); dispatch_async(cellSetupQueue, ^{ NSManagedObjectContext *newMoc = [[NSManagedObjectContext alloc] init]; NSPersistentStoreCoordinator *coordinator = [NSManagedObjectContext contextForCurrentThread].persistentStoreCoordinator; [newMoc setPersistentStoreCoordinator:coordinator]; NSNotificationCenter *notify = [NSNotificationCenter defaultCenter]; [notify […]

核心数据提取请求的复杂sorting

我有一个NSEntity ( Cloth )的属性( type ),这是从预定义的NSString值列表,如雨披,夹克,大衣,礼服,顶部,西装等填充。 我想要获取所有布料logging,以便logging按照定义的type顺序进行sorting。 即雨披的所有logging在上面,然后是外套,然后是外套,然后是礼服,然后是上衣,然后是西装等等。 Poncho‎ … Poncho‎ … Poncho‎ … Jacket … Jacket … Coat … Coat … Coat … Gown … Gown … Top … Top … Top … Top … Suit … Suit … 这个顺序不是升序或降序,而是定制的。 如何获取Clothlogging,以便按照(定义的)顺序返回它们?

核心数据 – 在向xcddatamodel文件添加额外的属性之后,应用程序崩溃

我在我的应用程序中使用核心数据。 对于数据,我用它链接sqlite文件。 我所有的数据都取得成功,我得到了正确的结果。 当我添加更多的属性到核心数据实体,然后运行应用程序,然后崩溃。 我的错误消息:“用于打开商店的模型与用于创build商店的模型不兼容” 所以可能是因为我改变了数据模型。 现在我应该怎么做,如果我想添加一些额外的领域,我的coredata模型没有应用程序崩溃?

NSFetchedResultsController不能使用瞬态属性

使用Xcode8.3在Swift 3中正常工作 我有一个项目正在进行,其中有保存消息的核心数据。 它按照时间对邮件进行分类,并按照date分类。 就是这样: let request = NSFetchRequest(entityName: "Message") let sortDiscriptor = NSSortDescriptor(key: "time", ascending: true) request.sortDescriptors = [sortDiscriptor] fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: mainThreadMOC, sectionNameKeyPath: "sectionTitle", cacheName: nil) fetchedResultsController.delegate = self do { try fetchedResultsController.performFetch() } catch { fatalError("Failed to initialize FetchedResultsController: \(error)") } 这是暂时的财产: var sectionTitle: String? { //this is **transient** property […]

NSFetchedResultsController不对encryption的属性进行sorting

我正在尝试使用可转换types对我的Core Data模型中的属性进行encryption,但是一旦数据被持久保存到sqlite数据库中,就会遇到很多试图读取结果的问题。 我的核心数据的主要应用程序是存储一些用户相关的数据,并重新取回它的名字,姓氏等sorting,然后显示在一个表视图使用首字母/姓氏作为部分标题。 看来,encryption干扰的结果,因为我什么也没有得到什么回来,当我尝试做NSFetchedResultsController使用lastName作为sorting描述符的获取请求。 我得到以下错误: CoreData: error: (NSFetchedResultsController) The fetched object at index X has an out of order section name 'S. Objects must be sorted by section name' 我花了很多时间来调查这个问题无济于事。 有谁知道这个问题可能是什么,以及如何解决这个问题? 在变压器解密数据之后,有没有办法在db上进行sorting呢? 或者,有没有办法可能找回原始数据,然后在NSFetchedResultsController的实例中有数据后进行sorting? 我使用这个来源来encryption核心数据属性:

Core Data不会触发NSManagedObject实例的错误,作为应用程序委托的属性

我将login的用户数据从服务器导入到名为“用户”的核心数据实体中。 我也将这个特定的用户对象引用到我的AppDelegate(作为一个属性),所以我可以在我的应用程序的其他地方访问它。 我面对的问题是,当我推另一个视图控制器,并尝试访问appdelegate.loggedInUser.id,我看到“ID”为零。 debugging器显示这个对象: $24 = 0x0b28ad30 <User: 0xb28ad30> (entity: User; id: 0xb261160 <x-coredata:///User/tC48E8991-B8A6-4E68-9112-93F9F21DB5382> ; data: <fault>) 我的理解是,当我尝试访问这个对象的某个属性时,Core Data框架将触发这个错误。 我很困惑,为什么我在这种情况下访问用户的“id”属性没有引发错误? 编辑 : 这是如何创build和使用loggedInUser对象: //method to get bgContext +(NSManagedObjectContext *)getContextOnBgWithParentSetToMainMOC { NSManagedObjectContext *tmpContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [tmpContext setParentContext:[Utils getAppDelegate].managedObjectContext]; return tmpContext; } //in App Delegate NSManagedObjectContext *bgContext = [NSManagedObjectContext getContextOnBgWithParentSetToMainMOC]; self.loggedInUser = [User importFromObject:loggedInUserData inContext:bgContext […]

核心数据 – fetchBatchSize被忽略

我正在build立一个NSFetchedResultsController,在请求上设置一个批处理大小,并执行获取。 根据性能,内存使用情况,日志logging(只要访问实体的属性)和SQL日志logging(在SQL查询中没有提及限制),显然fetchBatchSize被忽略,并且所有的对象都被提取。 造成这种情况的原因可能被忽略了? NSFetchRequest *request = [NSFetchRequest new]; NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context]; [request setEntity:entity]; [request setSortDescriptors:[[NSSortDescriptor alloc] initWithKey:@"dateCreated" ascending:NO]]; [request setFetchBatchSize:50]; self.fetchController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil]; NSError *error; [self.fetchController performFetch:&error]; 请注意,如果我在获取之前设置了一个断点,则该请求的确设置为批处理大小限制为50; 但SQL日志logging没有提到它。