使用RxJava将复杂对象保存到数据库

时间:2016-08-01 17:38:12

标签: android sqlite android-sqlite rx-java

我有一个应用程序向第三方API发出请求,需要将大部分数据保存到本地Sqlite数据库(其中本地主键是自动增量的)。

这些请求返回一个JSON响应,该响应通常包含许多嵌套对象,我需要映射并保存,然后将该记录链接回原始父对象 ObjectA(在本例中为)。

{
 "ObjectAList": [
   {
    "somefield1": "someValue1",
    "someField2": "someValue2",
    "nestedObjectB": {
        "nestedField": "nestedValue"
     },
     "nestedObjectCArray": [
      {
       "nestedArrayField": "nestedArrayValue"
       }
      ]
     }
    ]
 }

该应用程序由RxJava提供支持;对我来说,我相当新鲜。 (通过StorIO的网络和持久性都是被动的)。我能够设置方法来批量保存父 ObjectA ,并且工作正常。但是当我开始使用Observable.from迭代每个对象并随后保存嵌套的子对象,收集这些记录以及更新其各自父项中的外键(使用Observable.zip for多个孩子)。通常函数会被执行(无论如何根据我的日志),但不知何故结果不会通过链的其余部分提交回来,所以我不确定是否出现问题或者我的方法是否存在问题/实施

任何有关如何做到这一点的帮助或指示都将不胜感激!

编辑一些示例psudeo代码

/*this flatmap is attached to a network request that returns 
the previously defined JSON response*/

.flatMap(new Func1<List<ObjectAResponse>, Observable<List<ObjectAResponse>>>() {
        @Override
        public Observable<List<ObjectAResponse>> call(final List<ObjectAResponse> objectAResponse) {

            return saveObjectAListToDb(someOtherKey, objectAJsonResponses)                            
            .map(new Func1<PutResults<ObjectAEntity>, List<ObjectAResponse>>() {
                @Override
                public List<ObjectAResponse> call(PutResults<ObjectAEntity> putResults) {
                //Returning the original response to get the nested values
                return objectAResponse;
            }
        });
    }
})
.flatMap(new Func1<List<ObjectAResponse>, Observable<ObjectAResponse>>() {
            @Override
            public Observable<ObjectAResponse> call(List<ObjectAResponse> objectAResponse) {
                //Start emitting individual 'ObjectA' items to process its child items
                return Observable.from(objectAResponse);
            }
        })
.flatMap(new Func1<ObjectAResponse, Observable<ObjectAEntity>>() {
                @Override
                public Observable<ObjectAEntity> call(ObjectAResponse objectAResponse) {
                    /*Saving the individual nested objects in separate functions that return an observable that contains
                    * the resultant entity to be updated in to the main ObjectA entity
                    * (in reality there could be more nested child objects to save in parallel)
                    */
                    return Observable.zip(saveEpic(objectAResponse.nestedObjectB), saveUser(objectAResponse.nestedObjectCArray), 
                        new Func2<ObjectBEntity, ObjectCEntity, ObjectAEntity>() {

                        @Override
                        public Object call(ObjectBEntity objectBEntity, ObjectCEntity objectCEntity) {
                            //Function that updates the initial object with the new entities
                            return updateObjectA(objectAResponse.id, objectBEntity, objectCEntity);
                        }
                    });
                }
            })
.collect(new Func0<List<ObjectAEntity>>() {
                @Override
                public List<ObjectAEntity> call() {
                    return new ArrayList<ObjectAEntity>();
                }
            },
            new Action2<List<ObjectAEntity>, ObjectAEntity>() {
                @Override
                public void call(List<ObjectAEntity> objectAEntityList, ObjectAEntity o) {
                    //Collect the resultant updated entities and return the list
                    objectAEntityList.add(o);
                }
            });

0 个答案:

没有答案