Relationship equals 0 - ios

I'm new relationship on core data and do not know what's going on. I have a many to many relationship between the News and Friends entity as shown below:
News <<------------>> Friends
Where relationships are respectively:friends (in News) and news (on Friends)
Created NSManagedObject Subclass but did not change anything (do not know if it is essential to work the relationship) and also think it is not necessary popular this relationship since the two entities already have dados.E I discovered that the relationship is empty using:
let predicate = NSPredicate(format: "any friends.friendship.#count == %d" , 0)
where friendship is a Friends attribute ,and also I tried:
let predicateDenuncia = NSPredicate(format: "any friends.#count == %d" , 0)
That way I discovered my relationship is returning empty, because when equated to zero the condition of true and are shown the news ..... someone there knows what might be happening? Thank you.

Related

Return children of Realm RLMResults<Object> instead of <Object>'s when filtering

Example Realm relationship:
People.Dogs.FavouriteFoods
that are strictly one way -> RLMArrays
I have:
let result = RLMResult<People> from a previous operation.
and I have an array of FavouriteFood.IDs that a user selected
let selectedIDs: [String]
Now I am trying to filter/predicate this result, but instead of returning People, which I already have, I am trying to get out the FavouriteFood objects that intersect with the selectedIDs I can only find guides that explain how to sort/filter on RLMResults<People> where the result is People i.e. the same as the generic type on RLMResult.
My goal is to, in the end, construct a list where I can say "Out of the 14 FavouriteFoods Person A's Dogs have, 7 of them are in the selectedIDs list" etc. for Person B, C, D...
I want something like: "ANY dogs.favouriteFoods.ID in selectedIDs" but it should return all the FavouriteFoods matching the predicate for an individual Person, instead of all the People having Dogs having these particular favouriteFoods.
Is this possible to do as a predicate? Is there a way to flip the concept to ask for FavouriteFoods instead, or must I loop over all people, dogs, favouriteFoods and manually tally this up?
Thanks for any help given.

How to perform a compound query against to many relationships in CoreData

I am looking for some assistance in understanding NSPredicates and how to query CoreData. Here is what I have:
I have two entities: 1. Capture that represents a "capture" of some data and it has a to many relationship to 2. Identity tags.
I am looking to essentially get a Set of "Identity" tags from a particular Set of "Captures". i.e. I want to know all the unique identities of name 'X' for captures that are also tagged with an id of Int 'y'.
In swift I would model this like:
let captures: [Capture]
let identities = captures.flatmap({ $0.identities }).filter({ $0.id == "y" })
let uniqueIdentitiesOfParticularType: Set<Identity> = Set(identities.flatMap({ $0.name })
Any help please?
First, definitely start with the Predicate Programming Guide, which covers more of this in detail. See particularly "Using Predicates with Core Data."
Your solution is likely something along these lines if you wanted to be a bit careful about faulting:
let request = NSFetchRequest(entityName: "Capture")
let id = "y"
request.predicate = NSPredicate(format: "id == %#", id)
let identities = try context.executeFetchRequest(request) as NSArray
let names = identities.valueForKeyPath("#distinctUnionOfObjects.name")
Note that iOS 10 adds better bridges for this to Swift (you may not need as NSArray), but I haven't really explored them yet.
For more on #distinctUnionOfObjects and its friends, see Collection Operators.

Core Data Nested Relationship Fetch Predicate

OK so looked for answers and I'm probably making some assumption that others aren't and thats why the solution is working for them.
Problem:
Lets say I have an Entity called DogOwner. A dog owner can have multiple Dogs and each dog can have multiple Wearables. Note that all relationships are optional and one-to-many (inverse many-to-one).
Aim:
Search all the dog owners that own a dog that has a Wearable type GpsTracker and contains id of 898764.
My approach:
Fetching the entity DogOwner with the following predicate
NSPredicate(format: "(dog.wearable.type == %#) AND (dog.wearable.external_id CONTAINS[cd] %#)", theType, theIdentifier)
I get an unimplemented SQL generation for predicate error.
Tried -> with ANY (Nested core data fetch)
Tried other stuff present on the Internet. Nothing as far I have seen works, I assume that the problem is deeper down given that nested tree structured graph is being successfully queried by others using the dot notation.
While subquery is a feasible way to do this it is exceedingly complicated, and unnecessarily so. Why not simply search for the specific Wearable and get the owner via the convenient to-one relationships?
// fetch wearable with id x
let owner = wearable.dog.owner
I think you need to use SUBQUERY, if only because you need to test both conditions simultaneously:
NSPredicate(format: "SUBQUERY(dog, $d, SUBQUERY($d.wearable, $w, $w.type == %# AND $w.external_id CONTAINS[cd] %#).#count > 0).#count > 0", theType, theIdentifier)
Or "fetch DogOwners having more than 0 dogs having more than 0 wearables matching the criteria".

How to find entities with empty relationships in Core Data?

I have model with entities: PBOUser and PBOLocation.
There is relationship many to many. Every user may have a lot of locations and every location may belong to many users.
User and location has its own unique field: identifier.
It is very simple to find locations that belongs to a specific user:
let predicate = NSPredicate(format: "ANY users.identifier == %#", user.identifier)
if any user inside users field has identifier same as user.identifier... this is location I am looking for
But the question is:
How to find locations that not belong to any user?. In other words, their users field is empty.
Use the count of the relationship:
let predicate = NSPredicate(format: "users.#count == 0")

Predicate to fetch objects which parameter is contained in an specific NSSet

I have following entities:
PBOUser, PBOBusiness and PBOLocation.
PBOUser may have a lot of businesses.
PBOBusiness may have a lot of locations.
PBOLocation may belong to only one business.
PBOBusiness may belong to many users.
I need to find these locations which belong to those businesses that my user owns.
let predicate = NSPredicate(format: "business IN %#", myUser.businesses)
let locations = PBOLocation.MR_findAllWithPredicate(predicate) as? [PBOLocation]
But it doesn't work. How to do this in a quick way?
If you ever wanted to do it as a query you would need to use a SUBQUERY predicate
let predicate = NSPredicate(format: "SUBQUERY(business.users, $user, $user == %#).#count > 0", myUser)
business.users is the keyPath to the collection whose elements you want to test
Each item in the collection is evaluated agains the predicate $user == %#
When each item is evaluated against the predicate we use $user to make that the variable name for the single element to use in the predicate
The SUBQUERY will return a collection of results that match so to summarise we use #count > 0 to say if there was at least one match then this fits our criteria
You could probably come up with a predicate that works. But since you already have a reference to PBOUser, you're making things harder than they need to be. Consider that
PBOUser has a relationship to PBOBusiness
PBOBusiness has a relationship toPBOLocation`
With these relationships, a user's businesses are myUser.businesses, as in your code snippet. So take that one step farther and use the fact that locations are a property of the businesses:
let locations = myUser.businesses.valueForKey("location") as NSSet
This makes use of the fact that valueForKey on an array or set will call valueForKey on each member of the collection, and return the results as the same kind of collection. That's why it's an NSSet-- if you want a sorted array, add a call to sortedArrayUsingDescriptors:.

Resources