nsfetchrequest

NSPredicate, get results with a subset of one-to-many relationship

孤街浪徒 提交于 2019-12-01 00:35:21
I'mm working around with Core Data and NSFetchedResultsController . My Data Model looks like this: Product with one-to-many relationship called dataLines . The dataLine entity has a property name theWeek . I want to fetch all Product where dataLines.theWeek == someValue . This is easily done with a subquery. But this returns all dataLines. Is it possible to create a NSPredicate that returns the Product and a subset if dataLines only with the dataLines == someValue ? What you want to achieve could be reached in two ways: using a SUBQUERY [NSPredicate predicateWithFormat:@"SUBQUERY(dataLines, $x

Core-Data executeFetchRequest freezes App in a background thread

拜拜、爱过 提交于 2019-12-01 00:13:42
I have a saveMOC which is the direct parent of a mainMOC , and I need for online fetch another tmpMOC in order not to block my UI whilst fetching a ton of records from the Internet. My app freezes. I could narrow it to this very point: fetchedItems = [tmpMOC executeFetchRequest:fetchRequest error:&error]; I tried to enclose this within dispatch_sync , [moc.parentcontext.parentcontext.persistentStoreCoordinator lock/unlock] , [whatevermoc performBlockAndWait] ... I also try to fetch the _mainMOC ... No way... I understand that executeFetchRequest is not thread safe, so, how do I lock whatever I

Sorting NSFetchedResultsController by Swift Computed Property on NSManagedObjectSubclass

随声附和 提交于 2019-11-30 19:32:15
I'm building an app using Swift with Core Data. At one point in my app, I want to have a UITableView show all the objects of a type currently in the Persistent Store. Currently, I'm retrieving them and showing them in the table view using an NSFetchedResultsController . I want the table view to be sorted by a computed property of my NSManagedObject subclass, which looks like this: class MHClub: NSManagedObject{ @NSManaged var name: String @NSManaged var shots: NSSet var averageDistance: Int{ get{ if shots.count > 0{ var total = 0 for shot in shots{ total += (shot as! MHShot).distance

Core-Data executeFetchRequest freezes App in a background thread

Deadly 提交于 2019-11-30 18:11:22
问题 I have a saveMOC which is the direct parent of a mainMOC , and I need for online fetch another tmpMOC in order not to block my UI whilst fetching a ton of records from the Internet. My app freezes. I could narrow it to this very point: fetchedItems = [tmpMOC executeFetchRequest:fetchRequest error:&error]; I tried to enclose this within dispatch_sync , [moc.parentcontext.parentcontext.persistentStoreCoordinator lock/unlock] , [whatevermoc performBlockAndWait] ... I also try to fetch the

Performing multiplication (aggregation) with CoreData: how to?

两盒软妹~` 提交于 2019-11-30 17:50:09
问题 Following a fantastic tutorial by Jeff Lamarche, I'm trying to aggregate data for a specific subclass of NSManagedObject . This is the scenario. I created a class named Product that extends NSManagedObject class. Product class has three properties like the following: @property (nonatomic, retain) NSString* name; @property (nonatomic, retain) NSNumber* quantity; @property (nonatomic, retain) NSNumber* price; I also created a category, called Product+Aggregate , where I perform a sum

Swift Core Data - Request with distinct results

馋奶兔 提交于 2019-11-30 15:40:10
how I can call es request with distinct values in swift? This is my code: let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate let context: NSManagedObjectContext = appDelegate.managedObjectContext let request = NSFetchRequest(entityName: "MedicalData") request.propertiesToFetch = NSArray(object: "docID") request.returnsObjectsAsFaults = false request.returnsDistinctResults = true var results:NSArray = context.executeFetchRequest(request, error: nil) for data in results { var thisData = data as MedicalData println(thisData.docID) } I want to get distinct

NSPredicate 'The left hand side for an ALL or ANY operator must be either an NSArray or NSSet'

▼魔方 西西 提交于 2019-11-30 15:13:22
Not totally sure why this isn't working now, i thought it had been working previously. Does anyone see an issue with this FetchRequest construction? - (NSArray *)entriesForDate:(NSDate *)date { NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Entry"]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY addedOn.unique like %@", [T3Utility identifierForDate:date]]; request.predicate = predicate; NSError *error = nil; NSArray *matches = [self.database.managedObjectContext executeFetchRequest:request error:&error]; return matches; } Again, i'm 99% sure that this

NSPredicate Returns No Results with Fetch Request, Works with Array Filtering

梦想与她 提交于 2019-11-30 13:28:59
My situation is simple: I have some records in my core data store. One of their attributes is a string called "localId". There's a point where I'd like to find the record with a particular localId value. The obvious way to do this is with an NSFetchRequest and an NSPredicate. However, when I set this up, the request returns zero records. If, however, I use the fetch request without the predicate, returning all records, and just loop over them looking for the target localId value, I do find the record I'm looking for. In other words, the record is there, but the fetch request can't find it. My

coredata - fetch one attribute into an array

隐身守侯 提交于 2019-11-30 12:15:44
Aim: I would like to fetch the value of one attribute (from an entity) from the database (core data) into an array. Example Entity Name = Employees Attribute = employeeID I just want all the employeeIDs populated into an array / set. Question Given below is my implementation, I just feel it is kind of a round about way, I would like to know if there is a better way to do this. Code NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Employees"]; fetchRequest.resultType = NSDictionaryResultType; [fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"employeeID",

How to use binary flags in Core Data?

人盡茶涼 提交于 2019-11-30 11:43:11
问题 I have an int32 attribute in a Core Data database. I use this int as an enum bit field. Is it possible to create a NSPredicate to query items based on the binary value of this int ? Something like @"bitFieldAttribute & 0x0001" ? I'm also wondering if this is possible with a binary typed attribute ? 回答1: NSPredicate can handle it, but I'm not sure if CoreData will accept it as a valid predicate for execution on a data store. It might have trouble converting the bitwise operator into a SQL