2016-12-12 53 views
0

好的,我們先來解釋一下我的目標。 我正在嘗試構建一個結構來從Moya獲取物品,並使用領域對象正確映射我的物品以便將來保存它們。所有這些都應該使用rxswift完成,以獲得相關的反應性。Rxswift + Moya + Realm

這裏是我的2級:

import Foundation 
import ObjectMapper 
import RealmSwift 

final class GroupContainer: Mappable { 

    private(set) var items = List<Group>() 

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

    func mapping(map: Map) { 
     items <- (map["items"], transformation: RealmListTransform<Group>()) 

    } 
} 
import Foundation 
import RealmSwift 
import ObjectMapper 

final class Group: Object, Mappable { 

    private(set) dynamic var tag: String = "" 
    private(set) dynamic var name: String = "" 
    private(set) dynamic var groupLevel: Int = 0 
    private(set) dynamic var groupPoints: Int = 0 
    private(set) dynamic var members: Int = 0 

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

    func mapping(map: Map) { 
     tag <- map["tag"] 
     name <- map["name"] 
     clanLevel <- map["groupLevel"] 
     clanPoints <- map["groupPoints"] 
     members <- map["members"] 
    } 
} 

這裏是一個樣本結構來獲取和分析我的數據。這是RxMoya提供的例子。但是爲了使用ObjectMapper我不能使用mapObjectOptionnal方法。我嘗試將我的模型切換到ModelMapper,而不是找到一種方法將我的數組轉換爲列表 我還試圖找到替換爲mapObjectOptionnal,例如mapArray,mapObject,但由於某些原因,我不能直接使用它們之後提出我的要求。

import Moya 
import Moya_ObjectMapper 
import RxOptional 
import RxSwift 

struct TempFetcher { 
    let provider = RxMoyaProvider<APIService>() 
    let groupName = Observable<String> 

    func trackGroup() -> Observable<[Group]> { 
     return self.groupName.observeOn(MainScheduler.instance) 
        .flatMapLatest { search -> Observable<GroupContainer> in 
          return self.findGroup(name: search) 
        } 
        .flatMapLatest { container -> Observable<[Group]> in 
          return Observable<[Group]>.just(container.items) 
        } 
    } 

    private func findGroup(name: String) -> Observable<GroupContainer?> { 
     return self.provider 
      .request(APIService.Group(fullName: name)) 
      .debug() 
      .mapObjectOptional(type: Repository.self) 
    } 
} 

任何幫助,在我的研究進展將很樂意感謝! 謝謝。

回答

0

我用這些豆莢來實現相同和組合對我有用。

pod 'Moya/RxSwift' 
pod 'Moya-ObjectMapper/RxSwift' 
pod 'ObjectMapper+Realm' 

代替mapObjectOptional使用

return Mapper<LoginResponse>().map(JSONObject: data)