nspredicate

Check data exist before saving in coredata -Swift

笑着哭i 提交于 2019-12-04 06:10:28
问题 While saving the data am checking whether the data is already exist or not in the entity. Problem here is the above method is not working as expected, when I run the app for very first time it's showing data is already exist. Please check the below code and shed some light. Thanks. func someEntityExists(id: String, entityName: String, type : String, fieldName : String) -> Bool { let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: entityName) if type == "String"{ fetchRequest

Navigating the Core Data Object Graph [2]

时光总嘲笑我的痴心妄想 提交于 2019-12-04 05:43:02
问题 I asked a question yesterday where I really should have started with a simpler example. Having distilled my question to the basics, I've managed to solve my problem using existing SO questions and answers. I'm summarising my question here (and providing my own solution) because I don't think there are any posts that explain this clearly enough. Being new to Core Data and struggling to get away from SQL concepts, I'd welcome feedback on how appropriate my solution is, and if there are better

NSPredicate to filter only on “year” part of a date field

喜欢而已 提交于 2019-12-04 05:41:00
I have an entity with a date field and I would like to select the records for a given year. How to build a NSPredicate for the job? Didn't find anything about date functions (if any) in Core Data thanks A possible method: Step 1) See "Creating a Date from Components" from Apple's "Date and Time Programming Guide." Make an NSDate representing the beginning of the year, and an NSDate representing the end of the year. Step 2) Then you could build a predicate that searches for objects with date attrs that are greater than the first date and less than the last date. The predicate would look

NSSet use predicate to return objects matching given class

浪子不回头ぞ 提交于 2019-12-04 04:45:31
Let's say I have an NSSet that contains a collection of objects of type id<Shape> . . . of which there are CircleShape, SquareShape, HexagonalShape instances put into it (not the real protocol or class names) . . is it possible to use a predicate or another single line of code to return all of the instances of CircleShape? You can use a block-based predicate like this: NSSet *yourSet = ...; NSPredicate *pred = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) { return [evaluatedObject isKindOfClass:[CircleShape class]]; }]; NSSet *filteredSet = [yourSet

How to prevent “SQL Injection” in Core Data?

百般思念 提交于 2019-12-04 04:16:56
问题 I am building a pretty complex predicate in several iterations, and want to supply the matching values right away in the predicate. Instead of: [NSPredicate predicateWithFormat:@"departmentName like[c] %@"]; I want to do: NSString *str = [NSString stringWithFormat:@"departmentName like[c] '%@'", departmentName]; [NSPredicate predicateWithFormat:str]; Since this is a dumb substitution, I guess it's possible to "hack" the predicate accidently by entering garbage. I couldn't find anything that

NSPredicate syntax for DATEADD?

瘦欲@ 提交于 2019-12-04 04:08:35
问题 is there a way to do a DateAdd or a DateDiff function on an NSPredicate? Thank you, Jose. 回答1: Actually, there is! It's a roundabout way of doing it, because NSPredicate doesn't support it directly (ie, you can't just + anInterval to an NSDate ). Fortunately, you can do it, and luckily for you, I just figured it out about 2 days ago. To clarify: I'm assuming you're asking for something like: "an object has a date property. I want to see if this date property is some arbitrary interval before

Filter realm objects to only get one (distinct) object by attribute

独自空忆成欢 提交于 2019-12-04 04:03:28
问题 Let me explain first cause title may be a bit confusing. Say I have this realm objects of type Movie: Movie1(id: 0, genre: "horror") Movie2(id: 1, genre: "horror") Movie3(id: 3, genre: "sci-fi") What I need to do is get the first for every genre (in this case Movie1 and Movie3 ) I'd like to do it w/o loops using only realm + NSPredicate , so the performance is better, but I'm a bit stuck there... So far what I got is this: Realm().objects(Movie.self).sorted(byKeyPath: id, ascending: true)

NSPredicate to get children's children's

孤街醉人 提交于 2019-12-04 03:56:33
问题 I want to fetch all the treatments, that are children of this List object. I am not sure if it can be done. I have a TableView showing all the patients of a list. Each patient can have many treatments. I have a reference to the current list, and want to use that to somehow find the childrens children. Thank you 回答1: Use a fetch request for the "Treatment" entity with the predicate [NSPredicate predicateWithFormat:@"ofPatient.ofList = %@", currentList] using the inverse relationships from

iOS Compound Predicates

柔情痞子 提交于 2019-12-04 02:43:17
I am writing an application that has a database of photos. Each photo has several tags associated with it and the application has a search page with a lot of toggles that allows users to search for photos based on only the tags that are of interest to them. Each of these tags have stored integer ID's because they correspond to an external database's IDs, so I am trying to look them up simply by ID. All of the ID fields are indexed. The problem arrises when the predicates that I am writing become quite large because the user can select from lots of different tags to filter on. Those tags are

NSPredicate iPhone 3.2 SDK Core Data “IN clause” NSInvalidArgumentException exception

巧了我就是萌 提交于 2019-12-04 00:38:16
问题 I have a collection of Calendar objects and want to query them by their service_id property. I'm using Core Data on iPhone 3.2 SDK with sqlite. calendars is a NSArray result of a NSFetchRequest . I'm plucking the service_id property out of each object in the NSArray. NSPredicate *tripsWithServiceId = [NSPredicate predicateWithFormat:@"service_id IN %@", [calendars valueForKey:@"service_id"]]; However this generates an exception: 'NSInvalidArgumentException', reason: 'unimplemented SQL