CloudKit query for reference in array - ios

Let's say I have two CloudKit record types:
Company
name (string)
etc...
Employee
first name (string)
etc...
company (reference to Company)
In my app if I have an array Companies, is there a way to query CloudKit for all Employees whose company is in this array with a single query? Obviously I could go through the companies array and query one by one, but if this array is large then that's going to be a lot slower.
The docs say that you can reference an array in a CKQuery predicate, but doesn't say how.

Yes you can use the predicate
NSPredicate(“%k IN %#“, company, arrayofcompaniesReference)
company is the key in employee record type
arrayOfCompaniesReference is the array of reference of companies you have.

Related

Some questions about CloudKit

record type: Country(CKRecord)
--------------------
name: String
code: String
--------------------
record type: User(CKRecord)
------------------------
name: String
country: CKReference (reference to country record>
-------------------------
I have 2 record types (Country and User) created by CloudKit Dashboard. I encountered some cloudkit issues.
currently! I query User and then using country reference to fetch country record. When I using cloudKit to get some records
I have to do a lot of queries and fetches that waste a lot of time for waiting. so my issues are
1)How to get User record and Country record at the same time, If I query a record? example: query User Record
2)In some record that have a reference list. How to delete a CKReference in that reference list
Thanks a lot
In CloudKit there is no way to query 2 different recordTypes in 1 query. The only exception is when you already know the specific RecordId's. Then you could query for those ID's. So In your case, when you don't already know the User, you do have to do 2 queries to get the country record.
You could limit the number of fetches since you probably have a limited number of countries. At app startup just query all the countries and save them somewhere in memory, then when you do query for a user, you can get the country from memory. If you suspect that your country data will change frequently, then create a subscription so that you get a notification to update your data in memory.
If you have a reference list in your record, then query that record, remove the item from the list and save that record again.

How to fetch records from a class which has parent class reference?

I'm new to CoreData structure, I have two classes one is "Person.h" and another is "Education.h" which has one to many relations ship Person<--->> Education`.
Here's the attributes for each classes,
Person.h
personID (unique) Number
pName String
pAge Number
educations Set
here, p stands for person
Education.h
educationID (unique) Number
eName String
eState Number
eStarted String
eCompleted String
eCenterName String
eBy Person
here, e stands for education
Ok, now I want to fetch (all / some) education details for a Person. I've successfully inserted records in both the classes with proper inputs. How to get this done? Any suggestion? Please consider me to correcting, even if this flow would not clear to you (or its wrong).
Thanks,
Hagile
Normally you'd have a Core Data relationship on Person that points to the Education entity, configured as to-many. Then once you have an instance of Person, you just look up the value of that relationship like you'd look up the value of any property. You get back a collection of zero or more related Education instances, and you don't need to do an additional fetch.
Your eBy relationship on Education suggests that you're thinking of this as if you were working with SQL. With Core Data it's normal to have a to-many relationship defined on the entity that has the relationship (and indeed, eBy should really have an inverse relationship).

What is the best practice approach to fetching CKReferences in CloudKit?

In my CloudKit setup I have a Person record type and each person has a Friend attribute which is a CKReference to another Person object.
I fetch all Person CKRecords. I list these people in a table and list their friends with them. Currently I just take the CKReference and individually fetch each friend using the recordID.
This is the only way I can see to do it, but what would be best practice? Particularly as I am fetching potentially a very large number of friends. It seems counterintuitive to fetch all these objects individually.
I was wondering if there was a method much like CoreData, where you can choose to fetch related objects in the main fetch? So for example, when I fetch a Person it automatically fetched the friends too.
You could use an 'in' predicate query to fetch multiple id's. You can define a predicate like this:
NSPredicate(format: "To_ID in %#", [recordIdMe, recordIdOther])!
In your case where you see the recordIdMe and recordIdOther you should have an array of valid CKReference objects.
In CloudKit there is no functionality to automatically fetch related records.
Actually you should collect all the RecordIDs and add a CKFetchRecordsOperation to the relevant database object.
You get a back a dictionary to help you match the results with the original list.

Core Data Group By and Search for Properties of one to many relationship

I have an application with two core data entities:
Products and Properties.
Each product has a set of properties which is a one-to-many relationship in the model.
I want to do a few things:
A product has a brand_id, and a brand name. I want to get a list of all brand names, possibly by grouping all products by their brand_id.
I then want to get all unique keys from all products associated to that brand. So, select all products with a certain brand_id and then select all properties associated with any of the products, and then group them by their key.
After that, I want to select one particular key value pair to search for, and get all products associated with a property matching that key value pair.
Is any of this even possible with core data? I've been searching high and low, but it seems this problem is a little more complex than what most people have to do.
For first 2 questions I would suggest to create another Brand entity and make proper relations in your model. That will definitely make it much easier.
Make sure you have invert relation from Brand to products - that will automatically give you answer for first 2 questions.
You can search for distinct (unique) values in core data using:
[request setPropertiesToFetch:[NSArray arrayWithObject:#"distinctProperty"]];
For finding products that has properties matching key, value pair you should use this NSPredicate while fetching Product entities:
[NSPredicate predicateWithFormat:#"(SUBQUERY(properties, $property, $property.key == %# AND $property.value == %#).#count > 0)", #"keySearch", #"valueSearch"];

rails query serializable object

How do I query the database to find objects that contain one or more attributes that are stored as serializable?
For example, I have a concert which occurs only in certain cities. I want to make a Concert object with a column called cities and store an array of cities.
If I want to query my database to find all concerts that occur in 1 city (or all concerts that occur in an array of n cities), how do I do this?
The best way to do this isn't to store it in a serialized column, but a separate table called Cities. Then you can do this:
City.find_by_name('Cityname').concerts
One possible way to query would be to use SQL's LIKE condition. This would work for boolean conditions in serialized tables.
For example to find those Users with the 'notification' option on,
users=User.arel_table
User.where(users[:options].matches("%notification: true%"))
As for other type of variables this would not be as feasible.

Resources