Reputation: 95
I have two model class
class EntryModel: Object {
dynamic var id: Int = 0
dynamic var type: String = ""
dynamic var tap: Int = 0
dynamic var title: String = ""
dynamic var notes: String = ""
dynamic var startTime: Date? = nil
dynamic var endTime: Date? = nil
dynamic var amount1: Int = 0
dynamic var amount2: Int = 0
dynamic var meal: String = ""
dynamic var foodDetails: String = ""
dynamic var isAlarm: Bool = false
dynamic var timeOfAlarm: Date? = nil
//dynamic var lastFeedTime: Date? = nil // optionals supported
override static func primaryKey() -> String? {
return "id"
}
}
and
class EntryModelList: Object {
dynamic var id: Int = 0
dynamic var babyId: Int = 0
dynamic var date: Date? = nil
var entryModel = List<EntryModel>()
//dynamic var lastFeedTime: Date? = nil // optionals supported
override static func primaryKey() -> String? {
return "id"
}
}
I am new in realm so I don't know much about it. I want a query which will return me by filtering with babyId of EntryModelList and type of EntryModel. The which will match only this list I want.
I have tried with
let predicate = NSPredicate(format: "babyId == \(babyId) AND ANY entryModel.type IN %@", filterArray)
let entryModelList = try! Realm().objects(EntryModelList.self).filter(predicate).sorted(byKeyPath: "date", ascending: false)
Predicate output :
babyId == 1 AND ANY entryModelList.type IN {"breast", "bottle"}
Output:
Results<EntryModelList> (
[0] EntryModelList {
id = 2;
babyId = 1;
date = 2017-02-16 18:00:00 +0000;
entryModelList = RLMArray <0x6100000ff480> (
[0] EntryModel {
id = 8;
type = diaper;
tap = 1;
title = ;
notes = ;
startTime = 2017-02-17 11:26:36 +0000;
endTime = (null);
amount1 = 0;
amount2 = 0;
meal = ;
foodDetails = ;
isAlarm = 0;
timeOfAlarm = (null);
},
[1] EntryModel {
id = 10;
type = bottle;
tap = 1;
title = ;
notes = Fgdg;
startTime = 2017-02-16 23:44:22 +0000;
endTime = (null);
amount1 = 15;
amount2 = 0;
meal = ;
foodDetails = ;
isAlarm = 0;
timeOfAlarm = (null);
}
);
},
[1] EntryModelList {
id = 1;
babyId = 1;
date = 2017-02-14 18:00:00 +0000;
entryModelList = RLMArray <0x6100000fd680> (
[0] EntryModel {
id = 1;
type = breast;
tap = 0;
title = ;
notes = ;'l'k,;
startTime = 2017-02-14 23:15:49 +0000;
endTime = (null);
amount1 = 100;
amount2 = 0;
meal = ;
foodDetails = ;
isAlarm = 0;
timeOfAlarm = (null);
},
[1] EntryModel {
id = 4;
type = bottle;
tap = 0;
title = ;
notes = Jhkhjkhjk;
startTime = 2017-02-15 01:17:47 +0000;
endTime = (null);
amount1 = 100;
amount2 = 0;
meal = ;
foodDetails = ;
isAlarm = 0;
timeOfAlarm = (null);
},
[2] EntryModel {
id = 7;
type = breast;
tap = 0;
title = ;
notes = ;
startTime = 2017-02-15 11:43:08 +0000;
endTime = (null);
amount1 = 100;
amount2 = 0;
meal = ;
foodDetails = ;
isAlarm = 0;
timeOfAlarm = (null);
}
);
}
)
but this is not what I want. I want to know how I will get only those data list where EntryModel.type is "breast" or "bottle" type.
Thanks.
Upvotes: 1
Views: 468
Reputation: 18308
Results.filter(_:)
only allows for filtering which objects of the given type are included in the result set. It does not support filtering which objects are included in values of properties on those objects.
To do what you're after you'd need to use filter(_:)
on each of the entryModelList
properties of the objects you're working with to include only those you're interested in.
Upvotes: 1