From 4057676a0019135ea4bc1b17e04b638b6c285c0a Mon Sep 17 00:00:00 2001 From: Yaoda Wang Date: Fri, 14 Apr 2017 15:43:48 +0200 Subject: [PATCH] add close to each realm object. Bug fix but not test stablity. --- .../local/CompaniesLocalDataSource.java | 73 +++++++++++++---- .../source/local/PackagesLocalDataSource.java | 81 ++++++++++++++----- 2 files changed, 118 insertions(+), 36 deletions(-) diff --git a/mobile/src/main/java/io/github/marktony/espresso/data/source/local/CompaniesLocalDataSource.java b/mobile/src/main/java/io/github/marktony/espresso/data/source/local/CompaniesLocalDataSource.java index 69d897e..0c947e7 100644 --- a/mobile/src/main/java/io/github/marktony/espresso/data/source/local/CompaniesLocalDataSource.java +++ b/mobile/src/main/java/io/github/marktony/espresso/data/source/local/CompaniesLocalDataSource.java @@ -1,14 +1,33 @@ +/* + * Copyright(c) 2017 lizhaotailang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package io.github.marktony.espresso.data.source.local; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import java.util.List; +import java.util.concurrent.Callable; import io.github.marktony.espresso.data.Company; import io.github.marktony.espresso.data.source.CompaniesDataSource; import io.github.marktony.espresso.realm.RealmHelper; import io.reactivex.Observable; +import io.reactivex.ObservableSource; +import io.reactivex.functions.Function; import io.realm.Case; import io.realm.Realm; import io.realm.Sort; @@ -36,18 +55,31 @@ public static CompaniesLocalDataSource getInstance() { @Override public Observable> getCompanies() { - Realm rlm = RealmHelper.newRealmInstance(); - return Observable - .fromIterable(rlm.copyFromRealm(rlm.where(Company.class).findAllSorted("alphabet", Sort.ASCENDING))) - .toList() - .toObservable(); + return Observable.fromCallable(new Callable>() { + @Override + public List call() throws Exception { + Realm rlm = RealmHelper.newRealmInstance(); + List companyList = rlm.copyFromRealm(rlm.where(Company.class) + .findAllSorted("alphabet", Sort.ASCENDING)); + rlm.close(); + return companyList; + } + }); } @Override - public Observable getCompany(@NonNull String companyId) { - Realm rlm = RealmHelper.newRealmInstance(); - return Observable - .just(rlm.copyFromRealm(rlm.where(Company.class).equalTo("id", companyId).findFirst())); + public Observable getCompany(@NonNull final String companyId) { + return Observable.fromCallable(new Callable() { + @Override + public Company call() throws Exception { + Realm rlm = RealmHelper.newRealmInstance(); + Company company = rlm.copyFromRealm(rlm.where(Company.class) + .equalTo("id", companyId) + .findFirst()); + rlm.close(); + return company; + } + }); } @Override @@ -702,11 +734,17 @@ public void initData() { } @Override - public Observable> searchCompanies(@NonNull String keyWords) { - Realm rlm = RealmHelper.newRealmInstance(); - List results = rlm.copyFromRealm( - rlm.where(Company.class) - .like("name","*" + keyWords + "*", Case.INSENSITIVE) + public Observable> searchCompanies(@NonNull final String keyWords) { + + return Observable.fromCallable(new Callable>() { + @Override + public List call() throws Exception { + Realm rlm = RealmHelper.newRealmInstance(); + List companies = rlm.copyFromRealm(rlm.where(Company.class) + .like( + "name", + "*" + keyWords + "*", + Case.INSENSITIVE) .or() .like("tel", "*" + keyWords + "*", Case.INSENSITIVE) .or() @@ -714,9 +752,10 @@ public Observable> searchCompanies(@NonNull String keyWords) { .or() .like("alphabet", "*" + keyWords + "*", Case.INSENSITIVE) .findAllSorted("alphabet", Sort.ASCENDING)); - return Observable.fromIterable(results) - .toList() - .toObservable(); + rlm.close(); + return companies; + } + }); } } \ No newline at end of file diff --git a/mobile/src/main/java/io/github/marktony/espresso/data/source/local/PackagesLocalDataSource.java b/mobile/src/main/java/io/github/marktony/espresso/data/source/local/PackagesLocalDataSource.java index 83fee7a..c2e220f 100644 --- a/mobile/src/main/java/io/github/marktony/espresso/data/source/local/PackagesLocalDataSource.java +++ b/mobile/src/main/java/io/github/marktony/espresso/data/source/local/PackagesLocalDataSource.java @@ -1,9 +1,26 @@ +/* + * Copyright(c) 2017 lizhaotailang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package io.github.marktony.espresso.data.source.local; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import java.util.List; +import java.util.concurrent.Callable; import io.github.marktony.espresso.data.Package; import io.github.marktony.espresso.data.source.PackagesDataSource; @@ -48,10 +65,16 @@ public static void destroyInstance() { */ @Override public Observable> getPackages() { - Realm rlm = RealmHelper.newRealmInstance(); - - return Observable.just(rlm.copyFromRealm(rlm.where(Package.class) - .findAllSorted("timestamp", Sort.DESCENDING))); + return Observable.fromCallable(new Callable>() { + @Override + public List call() throws Exception { + Realm rlm = RealmHelper.newRealmInstance(); + List packageList = rlm.copyFromRealm(rlm.where(Package.class) + .findAllSorted("timestamp", Sort.DESCENDING)); + rlm.close(); + return packageList; + } + }); } /** @@ -62,11 +85,18 @@ public Observable> getPackages() { * @return The observable package from database. */ @Override - public Observable getPackage(@NonNull String packNumber) { - Realm rlm = RealmHelper.newRealmInstance(); - return Observable.just(rlm.copyFromRealm(rlm.where(Package.class) - .equalTo("number", packNumber) - .findFirst())); + public Observable getPackage(@NonNull final String packNumber) { + return Observable.fromCallable(new Callable() { + @Override + public Package call() throws Exception { + Realm rlm = RealmHelper.newRealmInstance(); + Package aPackage = rlm.copyFromRealm(rlm.where(Package.class) + .equalTo("number", packNumber) + .findFirst()); + rlm.close(); + return aPackage; + } + }); } /** @@ -171,6 +201,7 @@ public boolean isPackageExist(@NonNull String packageId) { RealmResults results = rlm.where(Package.class) .equalTo("number", packageId) .findAll(); + rlm.close(); return (results != null) && (!results.isEmpty()); } @@ -190,20 +221,32 @@ public void updatePackageName(@NonNull String packageId, @NonNull String name) { } @Override - public Observable> searchPackages(@NonNull String keyWords) { - Realm rlm = RealmHelper.newRealmInstance(); - return Observable.fromIterable(rlm.copyFromRealm( - rlm.where(Package.class) - .like("name", "*" + keyWords + "*", Case.INSENSITIVE) + public Observable> searchPackages(@NonNull + final String keyWords) { + + return Observable.fromCallable(new Callable>() { + @Override + public List call() throws Exception { + Realm rlm = RealmHelper.newRealmInstance(); + List packages = rlm.copyFromRealm(rlm.where(Package.class) + .like( + "name", + "*" + keyWords + "*", + Case.INSENSITIVE) .or() .like("companyChineseName", "*" + keyWords + "*", Case.INSENSITIVE) .or() - .like("company", "*" + keyWords + "*", Case.INSENSITIVE) + .like( + "company", + "*" + keyWords + "*", + Case.INSENSITIVE) .or() .like("number", "*" + keyWords + "*", Case.INSENSITIVE) - .findAll())) - .toList() - .toObservable(); + .findAll()); + rlm.close(); + return packages; + } + }); } -} \ No newline at end of file +}