Tag: 核心数据

如何使用predicateWithFormat进行查询NSString包含在NSSet中

使用StackMob作为后端。 我有一个Event实体有一个关系,称为用户users (键入NSSet )。 现在我想要得到某些用户的username是@"someuser"所有事件。 我的代码是这样的: [self.client getLoggedInUserOnSuccess:^(NSDictionary *result) { NSString *currentlyLoggedInUser = [result valueForKey:@"username"]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *eventEntity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedOjbectContext]; [request setEntity:eventEntity]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY users.username = %@", currentlyLoggedInUser]; NSLog(@"predicate: %@", predicate); [request setPredicate:predicate]; NSLog(@"request: %@", request); events = [NSArray new]; events = [self.managedOjbectContext executeFetchRequest:request error:nil]; […]

我应该在持续更新关系中使用FetchedResultsController作为对象吗?

我有一个简单的Coredata模型,其中一个实体叫做“对话”,另一个是“消息”。 基本上,我需要重现iPhone的短信应用程序。 我从消息到对话有一对一的关系,从对话到消息有一对多的关系。 Conversation{ messages<–>>Message.conversation } Message{ conversation<<–>Conversation.messages } 任何时候我启动我的应用程序,我所有的谈话都加载在我的fetchedResultsController。 如果我正确理解了Coredata是如何工作的,就像我有一种关系一样,每一条链接到我的对话的消息都会被加载。 我设置了一个批处理大小,这样我就不会同时加载所有的对话。 我的应用程序通过长时间的请求连接到服务器,所以我可以随时从服务器接收消息(将添加到coredata,然后使用NSNotification来告诉我的意见附加)。 我的问题是这样的:当我select一行时,我推动另一个视图在堆栈上,以便我可以看到我的消息。 我想知道如何做到这一点,原因是: •我可以通过我的观点来看我的NSSet通过关系给出的信息,对吧? 然而,正如我可以收到一条消息,同时在看这个观点,我如何刷新传递给视图的NSSet? •我也可以在该视图中使用另一个fetchedresultController,但在这种情况下,我将无故将我所有的消息预加载到先前的视图中? 我可以告诉coredata不要加载他们在我以前的观点? 我希望这是清楚的。 我有点绝望,我知道那个网站上有一些专家。 让我知道如果你需要别的东西,我会尽快提供更多的信息。

获取结果时发生崩溃控制器在保存后台MOC后更新表

我有一个连接到一个FRC(Fetched Results Controller)的tableView,我也有两个上下文,分别是在私有队列中初始化的backgroundContext和在主队列上初始化的mainContext。 我还设置了didSaveNotification将对象从一个上下文传递给另一个上下文。 当我在backgroundContext保存一些数据,它保存成功和FRC更新,但如果我再次重复该过程应用程序崩溃与一个错误 'ALL或ANY操作符的左侧必须是NSArray或NSSet。 保存是通过模态显示的forms完成的。 相同的viewController工作正常,如果出现在其他viewControllers。 但只在一个特定的一个崩溃。 但是再次提交表单没有其他额外的东西正在做。 这是我的整个崩溃报告。 由于未捕获exception“NSInvalidArgumentException”而终止应用程序,理由:'ALL或ANY操作符的左侧必须是NSArray或NSSet。 *第一个抛出调用栈:(0 CoreFoundation 0x0000000103575495 exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000102fbb99e objc_exception_throw + 43 2基金会 0x00000001003c706b – [NSPredicateOperator performOperationUsingObject:andObject:] + 826 3 Foundation 0x00000001003c6c1e – [NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 314 4 Foundation 0x00000001003c6ae2 – [NSPredicate evaluateWithObject:] + 19 5 CoreData 0x0000000102d61d06 – [NSFetchedResultsController(PrivateMethods)_objectInResults:] + 102 6 […]

如何回滚临时上下文的变化?

我创build一个像这样的temporaryContext: let temporaryContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType) temporaryContext.parentContext = Utility.managedObjectContext() temporaryContext.performBlockAndWait({ // .. here I have done some changes on temporaryContext let success = temporaryContext.save(nil) //GUI get updated, GUI use MAIN context }) 我想回滚更改,所以我这样做: temporaryContext.performBlockAndWait({ temporaryContext.rollback() let success = temporaryContext.save(nil) //GUI not get restored to the default variable }) 但它没有效果,父上下文不会回滚,为什么?

Objective-c中CoreData的自动索引

我必须为每个核心数据logging生成一个唯一的ID,有没有办法在核心数据中设置自动增量索引? 这样我们纯粹的sqlite! 如果不是的话,最好的策略是什么?

使用NSPredicate如何使用1个实体过滤来自核心数据的值

我有一个实体和三个属性,称为项目,比率,类别。 我想过滤来自category属性的值。 if (managedObjectContext==nil) { AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; managedObjectContext = appDelegate.managedObjectContext; } NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Restaurantmenu" inManagedObjectContext:managedObjectContext]; [request setEntity:entity]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"('category' contains[c] 'Fast Foood')"]; [request setPredicate:predicate]; NSLog(@"Pre %@",predicate); NSError *error = nil; NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; if (mutableFetchResults == […]

在同一个数据库上同时使用基于core-data和sqlite c的api

AFAIK CoreData可以configuration为在持久性存储中使用sqlite。 所以核心数据不提供使用SQL查询的方式。 所以core-data在后台有sqlite db。 同时使用基于Core-Data和Sqlite c的api访问相同的数据库是否安全? 会导致任何数据损坏是sqllite或任何线程问题。 为什么我要使用任何包装如FMDB或基于C的API是使用SQL查询来查询复杂的数据 请澄清我。

问题加载子实体mangedobject在coredata

我认为这是错误的,它只加载一个汽车部分:这种方法需要两个arrays之一与汽车的名字,一个零件,创build一个新的汽车,并添加汽车零件,然后将汽车保存到核心数据。 (目前不这样工作) for (int i=0; i<[massiveArray count]; i++) { //create a new car and part Car*newCar =(Car*)[NSEntityDescription insertNewObjectForEntityForName:@"Car" inManagedObjectContext:[self managedObjectContext]]; CarPart *part =[NSEntityDescription insertNewObjectForEntityForName:@"CarPart" inManagedObjectContext:[self managedObjectContext]]; //set car title to string title in array of titles [newCar setValue:[massiveArray objectAtIndex:i] forKey:@"name"]; //go through car parts array and add all new parts for that specific car for […]

UITableViewCellbutton标签在UISearchController中返回所需的IndexPath,而不是FetchedResultsController

我在Swift 2.0的Core Data项目的UITableView上实现了NSFetchedResultsController 。 另外,我有一个UISearchController实现。 除了我在自定义的UITableViewCellbutton上遇到的行为之外,一切都可以正常工作。 当UISearchController处于活动状态时, customTableViewCell的button按照它们应该的方式工作。 如果在fetchedResultsController显示其结果时单击相同的button,则方法认为索引0是发件人,无论我单击哪个button。 func playMP3File(sender: AnyObject) { if resultsSearchController.active { // ** THIS WORKS ** // get a hold of my song // (self.filteredSounds is an Array) let soundToPlay = self.filteredSounds[sender.tag] // grab an attribute let soundFilename = soundToPlay.soundFilename as String // feed the attribute to an initializer of […]

致命错误:值无法从Swifttypes桥接到Objective-Ctypes

我想使用新的NSBatchUpdateRequest写入一些信息到Core Data ,并不能找出为什么我得到这个错误。 有人能解释这里发生了什么吗? 这听起来像我有麻烦将Swift转换为Objective-Ctypes值? fatal error: value failed to bridge from Swift type to Objective-C type 这里是我的代码块,错误发生在: func updateUser(user: User) { var batchRequest = NSBatchUpdateRequest(entityName: "User") if doesUserExist(user.userId) { batchRequest.predicate = NSPredicate(format: "userId == %@", user.userId) } batchRequest.propertiesToUpdate = [ "userId" : user.userId, "username" : user.username, "email" : user.email, "fullName" : user.fullName, "gender" : […]