Skip to content
This repository has been archived by the owner on May 28, 2020. It is now read-only.

Commit

Permalink
add close to each realm object. Bug fix but not test stablity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaoda Wang committed Apr 14, 2017
1 parent 8c65cce commit 4057676
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -36,18 +55,31 @@ public static CompaniesLocalDataSource getInstance() {

@Override
public Observable<List<Company>> 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<List<Company>>() {
@Override
public List<Company> call() throws Exception {
Realm rlm = RealmHelper.newRealmInstance();
List<Company> companyList = rlm.copyFromRealm(rlm.where(Company.class)
.findAllSorted("alphabet", Sort.ASCENDING));
rlm.close();
return companyList;
}
});
}

@Override
public Observable<Company> getCompany(@NonNull String companyId) {
Realm rlm = RealmHelper.newRealmInstance();
return Observable
.just(rlm.copyFromRealm(rlm.where(Company.class).equalTo("id", companyId).findFirst()));
public Observable<Company> getCompany(@NonNull final String companyId) {
return Observable.fromCallable(new Callable<Company>() {
@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
Expand Down Expand Up @@ -702,21 +734,28 @@ public void initData() {
}

@Override
public Observable<List<Company>> searchCompanies(@NonNull String keyWords) {
Realm rlm = RealmHelper.newRealmInstance();
List<Company> results = rlm.copyFromRealm(
rlm.where(Company.class)
.like("name","*" + keyWords + "*", Case.INSENSITIVE)
public Observable<List<Company>> searchCompanies(@NonNull final String keyWords) {

return Observable.fromCallable(new Callable<List<Company>>() {
@Override
public List<Company> call() throws Exception {
Realm rlm = RealmHelper.newRealmInstance();
List<Company> companies = rlm.copyFromRealm(rlm.where(Company.class)
.like(
"name",
"*" + keyWords + "*",
Case.INSENSITIVE)
.or()
.like("tel", "*" + keyWords + "*", Case.INSENSITIVE)
.or()
.like("website", "*" + keyWords + "*", Case.INSENSITIVE)
.or()
.like("alphabet", "*" + keyWords + "*", Case.INSENSITIVE)
.findAllSorted("alphabet", Sort.ASCENDING));
return Observable.fromIterable(results)
.toList()
.toObservable();
rlm.close();
return companies;
}
});
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -48,10 +65,16 @@ public static void destroyInstance() {
*/
@Override
public Observable<List<Package>> getPackages() {
Realm rlm = RealmHelper.newRealmInstance();

return Observable.just(rlm.copyFromRealm(rlm.where(Package.class)
.findAllSorted("timestamp", Sort.DESCENDING)));
return Observable.fromCallable(new Callable<List<Package>>() {
@Override
public List<Package> call() throws Exception {
Realm rlm = RealmHelper.newRealmInstance();
List<Package> packageList = rlm.copyFromRealm(rlm.where(Package.class)
.findAllSorted("timestamp", Sort.DESCENDING));
rlm.close();
return packageList;
}
});
}

/**
Expand All @@ -62,11 +85,18 @@ public Observable<List<Package>> getPackages() {
* @return The observable package from database.
*/
@Override
public Observable<Package> getPackage(@NonNull String packNumber) {
Realm rlm = RealmHelper.newRealmInstance();
return Observable.just(rlm.copyFromRealm(rlm.where(Package.class)
.equalTo("number", packNumber)
.findFirst()));
public Observable<Package> getPackage(@NonNull final String packNumber) {
return Observable.fromCallable(new Callable<Package>() {
@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;
}
});
}

/**
Expand Down Expand Up @@ -171,6 +201,7 @@ public boolean isPackageExist(@NonNull String packageId) {
RealmResults<Package> results = rlm.where(Package.class)
.equalTo("number", packageId)
.findAll();
rlm.close();
return (results != null) && (!results.isEmpty());
}

Expand All @@ -190,20 +221,32 @@ public void updatePackageName(@NonNull String packageId, @NonNull String name) {
}

@Override
public Observable<List<Package>> searchPackages(@NonNull String keyWords) {
Realm rlm = RealmHelper.newRealmInstance();
return Observable.fromIterable(rlm.copyFromRealm(
rlm.where(Package.class)
.like("name", "*" + keyWords + "*", Case.INSENSITIVE)
public Observable<List<Package>> searchPackages(@NonNull
final String keyWords) {

return Observable.fromCallable(new Callable<List<Package>>() {
@Override
public List<Package> call() throws Exception {
Realm rlm = RealmHelper.newRealmInstance();
List<Package> 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;
}
});
}

}
}

0 comments on commit 4057676

Please sign in to comment.