2017-02-15 211 views
0

我有兩個模型類斯威夫特3需要幫助的境界查詢

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" 
    } 
} 

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" 
    } 
} 

我在新的境界,所以我不很瞭解。我想要一個查詢,它將通過使用EntryModelList的babyId和EntryModel類型進行過濾來返回給我。這將只匹配我想要的這個列表。

我試圖與

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) 

謂詞輸出:

babyId == 1 AND ANY entryModelList.type IN {"breast", "bottle"} 

輸出:

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); 
      } 
     ); 
    } 
) 

但這不是我想要的。我想知道我將如何獲得EntryModel.type爲「乳房」或「瓶子」類型的那些數據列表。

感謝。

+0

你能提供一些示例數據顯示你希望你的查詢匹配vs它實際匹配什麼?從描述中很難理解你希望查詢做什麼不同。 – bdash

+0

@bdash我已更新我的問題,請讓我知道您可以理解我的問題。我的英文不是很好。如果你能給我一個解決方案,我會對你謙虛。謝謝 – Jihan

+0

你的意圖是最終得到一個'Results ',其中每個包含'EntryModelList'的'entryModelList'屬性被過濾,因此它只包含具有適當'type'屬性的對象? – bdash

回答

1

Results.filter(_:)只允許過濾給定類型的哪些對象包含在結果集中。它不支持過濾哪些對象包含在這些對象的屬性值中。

要做到你以後,你可能需要使用filter(_:)每個你正在使用的對象entryModelList屬性只包括那些你感興趣的內容。