2016-06-16 8 views
1

ええ、それは解決策ですが、以下のケースでは完璧ではありません。 JSONスキーマとして、私は次のモデルRealmSwiftコントロール反映プロセス

class Advertising: Object, Mappable { 

    dynamic var id    = "" 
    dynamic var key   = "" 
    private var data   = [AdvertisingData]() 
    { 
     willSet{// **i have to do this for Store it, it will be empty in realm if not do this** 
      dataList.removeAll() 
      dataList.appendContentsOf(newValue) 
     } 
    } 
    let dataList = List<AdvertisingData>() 

    convenience required init?(_ map: Map) { 
     self.init() 
    } 

    func mapping(map: Map) { 
     id <- map["id"] 
    key <- map["advertising_key"] 
    data <- map["advertising_data"] 
    } 

    override class func ignoredProperties() -> [String] { 
     return ["data"] 
    } 

} 

class AdvertisingData: Object, Mappable { 

    dynamic var id    = "" 
    dynamic var title   = "" 
    dynamic var theDescription = “” 

    convenience required init?(_ map: Map) { 
     self.init() 
    } 

    func mapping(map: Map) { 
     id <-    map["id"] 
     title <-   map["title"] 
     theDescription <- map["description"] 
    } 

} 
  1. を作成するので、私は、単にデータリストへの同期データの
  2. をクラスの広告でデータを宣言する必要があり、マッピングにObjectMapperを使用

    JSON from server 
    { 
        id: "4", 
        key: "web_banner", 
        data: [ 
         { 
          id: "11", 
          title: "app", 
          description: "", 
         } 
        ] 
    } 
    

  3. 私はプロパティデータを無視する必要があります これは私のソリューションです、あまりにも醜い、いくつかの良いアイデアですか? はRealm docsから事前に

答えて

3

ありがとう:

あなたのJSONスキーマがあなたのレルムのオブジェクトと正確に一致していない場合、我々はあなたがあなたのJSONを変換するために、サードパーティのモデルマッピングフレームワークを使用することをお勧め。 Swiftには、Realmで動作する積極的に維持されているモデルマッピングフレームワークが豊富に用意されています。その一部はrealm-cocoa repositoryにリストされています。

このフレームワークの1つをマッピングに使用してください。

+0

これは解決策ですが、このケースでは完璧ではありません。初期の質問が変更されたので、私は質問を変更して –

+0

@巩小caseを変更しました。この質問を見ることをお勧めします:http://stackoverflow.com/questions/33804181/alamofire-objectmapper-realm-nested-objects – Dmitry

+0

ありがとう、私は私のための最高のソリューションを見つけた! –

関連する問題