Roberto
Roberto

Reputation: 1590

How to make simple join in relationship of Core Data Swift

I have Core Data with relationship:

extension Rating {

    @NSManaged var date: NSString?
    @NSManaged var rating: NSNumber?
    @NSManaged var ratenames: RateNames?

}

extension RateNames {

    @NSManaged var name: String?
    @NSManaged var rating: NSSet?

}

And I need to select all values from RateNames with all data from Rating linked to RateNames from a selected date.

For example RateNames data:

name=a; name=b;

For example RateNames data:

date=10-02-2016; rating=5; linked to a
date=10-02-2016; rating=3; linked to b
date=09-02-2016; rating=2; linked to a
date=08-02-2016; rating=3; linked to a
date=08-02-2016; rating=1; linked to b

For example I need result for 08-02-2016 Result should be:

{(name:a, date:08-02-2016, rating=3), (name:b, date:08-02-2016, rating=1)}

result for 09-02-2016 date should be the following:

{(name:a, date:09-02-2016, rating=2), (name:b, date:nil, rating=nil)}

I also do need b in results because I need to see whether it is null or not.

I am trying to do something like:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "RateNames")
do {
    let results = try managedContext.executeFetchRequest(fetchRequest)
    for result in results {
let child = result. ..some var here.. as! Rating

But cannot get how to implement this

Upvotes: 0

Views: 3110

Answers (2)

pbasdf
pbasdf

Reputation: 21536

It's easier to get what you want by fetching the Rating objects, and using the relationship to get the name of the related RateName objects:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Rating")
do {
    let results = try managedContext.executeFetchRequest(fetchRequest) as! [Rating]
    for result in results {
        let name = result.ratenames.name
        let date = result.date
        let rating = result.rating
        print("name:\(name), date:\(date), rating:\(rating)")
    }
}

This will print results similar to your expected results, though it won't include the (name:b, date:nil, rating=nil).

Update

If you want to limit the above to a given date, you can use a predicate for the fetch request:

fetchRequest.predicate = NSPredicate(format:"date == %@", chosenDate)

But since you want to include, (name:b, date:nil, rating=nil), you will need to fetch ALL the RatingName objects. The rating property of each object will include any and all Ratings to which it is related. So I think you will then need to filter them to identify any for the correct date:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "RateNames")
do {
    let results = try managedContext.executeFetchRequest(fetchRequest)
    let datePredicate = NSPredicate(format:"date == %@", chosenDate)
    for result in results {
        let filteredRatings = result.rating.filteredSetUsingPredicate(datePredicate)
        if filteredRatings.count > 0 {
            let rating = filteredRatings.anyObject()
            print("name:\(result.name), date:\(rating.date), rating:\(rating.rating)")
        } else {
            print("name:\(result.name), date:nil, rating:nil")
        }
    }
}

This assumes there can be only one Rating for each RatingName for a given date; if not you will have to iterate through the filteredRatings set.

Upvotes: 2

Abhishek Sharma
Abhishek Sharma

Reputation: 3283

you need to just setup relationship in Datamodel like i have created for following example

From your code i found that you didn't mentioned One-to-Many Relationship .

enter image description here enter image description here enter image description here

Then create again sub class of NSManageObject. xCode will create all needed variables for same.

Upvotes: 0

Related Questions