Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample app demonstrating offline mode #1960

Merged
merged 51 commits into from Feb 19, 2016
Merged

Conversation

cmelchior
Copy link
Contributor

Sample app demonstrating usage of Realm in a app that should work offline.

It uses the New York Times API to show a list of news items. Each item will be marked read after being visible for 2 seconds.

Simpel MVP architecture
Separation between Model/Repository
Uses RxJava/Retrofit/Jackson

Conflicts:
	realm/gradle/wrapper/gradle-wrapper.properties
	realm/realm-library/src/main/java/io/realm/BaseRealm.java
	realm/realm-library/src/main/java/io/realm/RealmObject.java
	realm/realm-library/src/main/java/io/realm/RealmResults.java
Conflicts:
	realm/gradle/wrapper/gradle-wrapper.properties
	realm/realm-library/src/main/java/io/realm/BaseRealm.java
Conflicts:
	realm/realm-library/build.gradle
Conflicts:
	changelog.txt
	examples/gradle/wrapper/gradle-wrapper.properties
	realm/realm-library/src/main/java/io/realm/RealmQuery.java
Conflicts:
	examples/settings.gradle
	realm/realm-jni/src/io_realm_internal_TableQuery.h
	realm/realm-library/src/androidTest/java/io/realm/RxJavaTests.java
	realm/realm-library/src/main/java/io/realm/Realm.java
	realm/realm-library/src/main/java/io/realm/RealmObject.java
	realm/realm-library/src/main/java/io/realm/rx/RealmObservableFactory.java
	realm/realm-library/src/main/java/io/realm/rx/RxObservableFactory.java
@cmelchior
Copy link
Contributor Author

@cmelchior cmelchior self-assigned this Jan 26, 2016
@bmunkholm bmunkholm added the T-Doc label Feb 2, 2016
@cmelchior cmelchior added T-Doc and removed T-Doc labels Feb 10, 2016
@cmelchior
Copy link
Contributor Author

Looked like this PR has gotten a bit stale. Everything should be ready for review @realm/java


<Spinner
android:id="@+id/spinner"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty line

@cmelchior
Copy link
Contributor Author

Imbarrassing. I can reproduce the crash:

  NewsReaderApplication  E  java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only 
                            be accessed on the thread they were created.
                         E  java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` 
                            handling.
         AndroidRuntime  E  FATAL EXCEPTION: RxComputationThreadPool-3
                         E  Process: io.realm.examples.newsreader, PID: 7304
                         E  java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` 
                            handling.
                         E      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:60)
                         E      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
                         E      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                         E      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Sched
                            uledThreadPoolExecutor.java:152)
                         E      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThr
                            eadPoolExecutor.java:265)
                         E      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                         E      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                         E      at java.lang.Thread.run(Thread.java:818)
                         E  Caused by: rx.exceptions.OnErrorNotImplementedException: Realm access from incorrect thread.
                             Realm objects can only be accessed on the thread they were created.
                         E      at rx.Observable$27.onError(Observable.java:7923)
                         E      at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:159)
                         E      at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
                         E      at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:146)
                         E      at rx.internal.operators.OnSubscribeTimerOnce$1.call(OnSubscribeTimerOnce.java:49)
                         E      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
                         E      ... 7 more
                         E  Caused by: java.lang.IllegalStateException: Realm access from incorrect thread. Realm object
                            s can only be accessed on the thread they were created.
                         E      at io.realm.BaseRealm.checkIfValid(BaseRealm.java:377)
                         E      at io.realm.Realm.executeTransaction(Realm.java:1065)
                         E      at io.realm.examples.newsreader.model.Repository.updateStoryReadState(Repository.java:10
                            9)
                         E      at io.realm.examples.newsreader.model.Model.markAsRead(Model.java:103)
                         E      at io.realm.examples.newsreader.ui.details.DetailsPresenter$2.call(DetailsPresenter.java
                            :68)
                         E      at io.realm.examples.newsreader.ui.details.DetailsPresenter$2.call(DetailsPresenter.java
                            :65)
                         E      at rx.Observable$27.onNext(Observable.java:7928)
                         E      at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139)
                         E      ... 9 more

@zaki50
Copy link
Contributor

zaki50 commented Feb 16, 2016

crash after opening DetailsActivity

java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.

02-16 20:06:37.527 24294-27771/io.realm.examples.newsreader E/NewsReaderApplication: java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
02-16 20:06:37.528 24294-27771/io.realm.examples.newsreader E/NewsReaderApplication: java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.

                                                                                     --------- beginning of crash
02-16 20:06:37.529 24294-27771/io.realm.examples.newsreader E/AndroidRuntime: FATAL EXCEPTION: RxComputationThreadPool-3
                                                                              Process: io.realm.examples.newsreader, PID: 24294
                                                                              java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.
                                                                                  at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:60)
                                                                                  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
                                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
                                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                  at java.lang.Thread.run(Thread.java:818)
                                                                               Caused by: rx.exceptions.OnErrorNotImplementedException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
                                                                                  at rx.Observable$27.onError(Observable.java:7923)
                                                                                  at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:159)
                                                                                  at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
                                                                                  at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:146)
                                                                                  at rx.internal.operators.OnSubscribeTimerOnce$1.call(OnSubscribeTimerOnce.java:49)
                                                                                  at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
                                                                                  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423) 
                                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                                                                                  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269) 
                                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                                  at java.lang.Thread.run(Thread.java:818) 
                                                                               Caused by: java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
                                                                                  at io.realm.BaseRealm.checkIfValid(BaseRealm.java:377)
                                                                                  at io.realm.Realm.executeTransaction(Realm.java:1065)
                                                                                  at io.realm.examples.newsreader.model.Repository.updateStoryReadState(Repository.java:109)
                                                                                  at io.realm.examples.newsreader.model.Model.markAsRead(Model.java:103)
                                                                                  at io.realm.examples.newsreader.ui.details.DetailsPresenter$2.call(DetailsPresenter.java:68)
                                                                                  at io.realm.examples.newsreader.ui.details.DetailsPresenter$2.call(DetailsPresenter.java:65)
                                                                                  at rx.Observable$27.onNext(Observable.java:7928)
                                                                                  at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139)
                                                                                  at rx.internal.operators.OnSubscribeTimerOnce$1.call(OnSubscribeTimerOnce.java:49) 
                                                                                  at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) 
                                                                                  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423) 
                                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                                                                                  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269) 
                                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                                  at java.lang.Thread.run(Thread.java:818) 
02-16 20:06:38.866 24294-24294/io.realm.examples.newsreader D/NYTimesDataLoader: Success - Data received: home
02-16 20:09:56.614 24294-27771/io.realm.examples.newsreader I/Process: Sending signal. PID: 24294 SIG: 9

android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to remove these paddings.

@dalinaum
Copy link
Contributor

@zaki50 said that it encountered the following error.

Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.

Repository() is initialized on @UiThread https://github.com/realm/realm-java/pull/1960/files#diff-0df357d98f97bc637ce62b4a514ba538R56 and updateStoryReadState() is invoked on @UiThread https://github.com/realm/realm-java/pull/1960/files#diff-0df357d98f97bc637ce62b4a514ba538R107 too.

Does @UiThread annotation guarantee that methods with this annotation invoke on UI Thread?

}

/**
* Initialize Timer logging
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Timer -> Timber

@zaki50
Copy link
Contributor

zaki50 commented Feb 16, 2016

@dalinaum @UiThread annotation is only a marker for lint. No guarantee for execution of UI thread.

see http://tools.android.com/tech-docs/support-annotations

@cmelchior
Copy link
Contributor Author

Ups, found the crash. It was a Scheduler issue, not sure how I missed that 😕 Also moved away from deprecated transaction API's

@realm/java

@dalinaum
Copy link
Contributor

👍

@cmelchior
Copy link
Contributor Author

@dalinaum 🍰

cmelchior added a commit that referenced this pull request Feb 19, 2016
Add sample app demonstrating offline mode
@cmelchior cmelchior merged commit 99a9ca4 into master Feb 19, 2016
@cmelchior cmelchior deleted the cm/offline-newsreader branch February 19, 2016 07:33
@Drjacky
Copy link

Drjacky commented Nov 26, 2017

@cmelchior Thank you for the sample app, but the API doesn't work!
api.nytimes.com

{"request_path":"/","message":"API not found with these values","request_host":["api.nytimes.com"]}

D/NYTimesDataLoader: Failure: Data not loaded: health - java.net.UnknownHostException: Unable to resolve host "api.nytimes.com": No address associated with hostname

@beeender
Copy link
Contributor

@Drjacky It should be your network issue. It is working here (just tested with the apk)

➜  ✗ ping api.nytimes.com 
PING api.nytimes.com (34.234.50.162) 56(84) bytes of data.

@Drjacky
Copy link

Drjacky commented Nov 28, 2017

@beeender Yea, you're right. But it's weird that it doesn't work either via VPN!

Pinging infodev-kong-v3-prd-444147425.us-east-1.elb.amazonaws.com [54.172.233.25
5] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Ping statistics for 54.172.233.255:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

Thanks anyway to informed me.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants