Retrieving just the joined entities from Core Data - core-data

I have a movie showtimes entity which has a one-to-one relationship to a movie entity. The inverse (movie -> movie showtime) relationship is a one-to-many relationship. If a movie is deleted, the associated movie showtimes will also be deleted, but if a movie showtime is deleted the associated movie will stay. (Not sure how much of that is relevant but wanted to clarify the situation as much as I could)
Now, is there a way to query Core Data to get only the unique movies for which I have showtimes?
Is it possible to select from the movie showtimes and somehow restrict the results to just the associated unique movies? Or would selecting from the movie entity bring back only the movies with a matching row in the movie showtime entity?

Sure. Write a fetch request on Movie with no restrictions, and you'll get all Movie instances.

Taking your questions in turn:
Now, is there a way to query Core Data to get only the unique movies for which I have showtimes?
You could use a predicate to select movies where the count of showtimes is greater than zero:
"showtimes.#count > 0"
Is it possible to select from the movie showtimes and somehow restrict the results to just the associated unique movies?
If you have (courtesy of your first query) an array (say scheduledMovies) of movies which have showtimes, then you can fetch the associated showtimes using a predicate like this:
"movie IN %#", scheduledMovies
Or would selecting from the movie entity bring back only the movies with a matching row in the movie showtime entity?
If you fetch movies, then you will get ALL movies, unless you specify a predicate as per your first question. But if you fetch showtimes, you can get an array of the associated movies using key value coding with the key:
"#distinctUnionOfObjects.movie"
The resulting array will not have any movies with no showtimes. (The first part of this key removes any duplicates, since several showtimes might have the same movie).

Related

Search collection fields and populated fields at the same time

I have 2 collections as the following:
Products
- name (String)
- price (number)
- category (ObjectID refs to Category collection)
Category
- name
And we have search query for example "some query"
I'm trying to get the records where product.name or product.category.name partially includes the search query
If you are using Mongoose populate, that uses DBRef, which is not easily used on the server side.
If the Products document contains only the _id of the corresponding Category document, you would need to use aggregation to read all Products documents, and for each one load all of the corresponding Category documents, then filter using a regular expression.
That is not scalable.
If you denormalize a little bit by having the Products document contain both the _id and name of the Category (assuming Category actually has other data), you could use a text index on the Products collection, or if you are using Atlas, full text search in the Products collection.

Swift4 Core Data how to insert into NSOrderedSet by date?

I have a core data model with one record having a to many relationship to photos entity. The relationship is flagged as ordered in the data model.
The code below adds a new record at the last index. I want records to be ordered by date - meaning I have to insert the object at some specific index
let photo = NSEntityDescription.insertNewObject(forEntityName: photoKey, into: self.managedObjectContext()) as! Photo
Is there a way to have core data perform a block or some operation to determine the correct insertion index for the new record? Or do I have to calculate it myself?
I'm concerned about order because photo deletions force the need to recalculate the date range of all photos.

AngularFire2 > Firestore > Complex queries with `in` or not `in`

I've been following this tutorial to build a rating system.
Which suggests the following collections:
Movies:
uid
title
Users
uid
name
Stars
movieId
userId
value
Now, suppose I wanted to display only pending movies to be rated by users.
How could this query be written?
If you think you'll actually have a small number of pending and not an imdb sized list you could
Get all movies
Get all movies rated by this user
Use the two collections to firm a list of movies they need to review
Or
1. Add a sub collection to user called unrated movies
1. every time a niche is added to the db add it to every user's collection
1. When a user reviews a movie delete from collection
Or have a value in the stars db of - 1 to represent unrated, add that for all users and all movies, then query by user ID and value is - 1

Predicate subquery for products associated by tag

I have a datamodel in core data as such:
Store <--->> Product <<--->> Tag
I'm looking to create a query that matches products based on their associated tags. A typical use scenario would be you choose to search storeA and output storeB. By selecting storeA a list of storeA products is presented. Let's say you pick productA from the list which brings you to a second viewcontroller. That second viewcontroller lists all relating productB's (because earlier you set storeB as output). I'm following this: predicate subquery to return items by matching tags but want the added store filter of products. Is subquery the way to go? How would I include a filter by selected Store?

Cassandra Data Model Design

Suppose I have have a huge list of movies categorized by genres. Users can vote for movies, and each movie can be in multiple genres.
What is a good way to store this in Cassandra if I want to present the top X movies per category? Please ignore other use cases as I can have other column families as required (like presenting detailed movie information).
Action
Movie A
Movie B
Movie C
Comedy
Movie D
Movie E
Movie A
Based on the information you have presented -I say that you only gave requirements to create a single column family.
The columns would be - movie name, category, and any other attributes about the movie.
It is quite common to create several column families with different structure that are like 'materialized views' of original column family.
In Cassandra you design column families based on how the application is going to use it. So, you design your queries first then you design the column family to support it.

Resources