如何使用Alamofire和RxSwift编写它?

时间:2016-11-08 14:51:27

标签: realm alamofire rx-swift

  

如何使用Alamofire和RxSwift编写它?我试着用   RxAlamofire,但它看起来很奇怪   如何使用Alamofire和RxSwift编写它?我试着用   RxAlamofire,但它看起来很奇怪

我的Json%

"session": {

   "sessionToken":  "16mpmthgkj6vjcco9u073fe61njhikbcn47s1co7bsfg110oefn4",  
   "refreshToken": "4ap0dqnuttpm5pv18njhthqv2d",  
   "expirationDate":"2016-10-17 03:44:28.826 +0000"       
},   
"profile":  
{

   "userId": "abUser$gT5d67g",  
   "userPhone": 380995392020,  
   "createdAt": "2016-08-14 03:44:28.826 +0000"  
}


    let registraion = request(.post, baseUrlDev + kRegUrl, parameters: phoneData, encoding:  JSONEncoding.prettyPrinted, headers: headers)
        .flatMap {
            $0
                .validate(statusCode: 200 ..< 300)
                .validate(contentType: ["application/json","text/html"])
                .rx.responseJSON()
        }

        .observeOn(MainScheduler.instance)
        .subscribe(onNext: {  response in
            let data = response.1
            let json = JSON(data)
            let sessionArray = json["session"]
            let userArray  = json["profile"]
            print(json)
           //here covert to json and save as realm object
            do{
                let session = Session()
                session.mToken = sessionArray["authToken"].string
                session.mRefreshToken = sessionArray["refreshToken"].string
                session.mExpirationDate = sessionArray["expirationDate"].string
                session.mExpired = false

                try realm.write {
                    realm.add(session, update: true)
                }
            }
            catch let err as NSError {
                print("Error with realm: " + err.localizedDescription)
            }


        }).addDisposableTo
    }

0 个答案:

没有答案