How to read file from asset folder in android using RX in background threadHow do save an Android Activity state using save instance state?How to check if a service is running on Android?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I pass data between Activities in Android application?How do I display an alert dialog on Android?Download a file with Android, and showing the progress in a ProgressDialogIs there a way to get the source code from an APK file?How do I rotate the Android emulator display?How to manage startActivityForResult on Android?What's “tools:context” in Android layout files?
How to get directions in deep space?
Should I warn a new PhD Student?
Check if object is null and return null
Possible Eco thriller, man invents a device to remove rain from glass
Why does a 97 / 92 key piano exist by Bösendorfer?
What's the name of the logical fallacy where a debater extends a statement far beyond the original statement to make it true?
What should be the ideal length of sentences in a blog post for ease of reading?
How do I fix the group tension caused by my character stealing and possibly killing without provocation?
If Captain Marvel (MCU) were to have a child with a human male, would the child be human or Kree?
Has the laser at Magurele, Romania reached a tenth of the Sun's power?
Origin of pigs as a species
What is the smallest number n> 5 so that 5 ^ n ends with "3125"?
What happens if I try to grapple mirror image?
Why is participating in the European Parliamentary elections used as a threat?
Is there a RAID 0 Equivalent for RAM?
Animation: customize bounce interpolation
Telemetry for feature health
Is there a reason to prefer HFS+ over APFS for disk images in High Sierra and/or Mojave?
Do I have to take mana from my deck or hand when tapping a dual land?
Would this string work as string?
How to preserve electronics (computers, iPads and phones) for hundreds of years
Anime with legendary swords made from talismans and a man who could change them with a shattered body
Can I say "fingers" when referring to toes?
How to make a list of partial sums using forEach
How to read file from asset folder in android using RX in background thread
How do save an Android Activity state using save instance state?How to check if a service is running on Android?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I pass data between Activities in Android application?How do I display an alert dialog on Android?Download a file with Android, and showing the progress in a ProgressDialogIs there a way to get the source code from an APK file?How do I rotate the Android emulator display?How to manage startActivityForResult on Android?What's “tools:context” in Android layout files?
private ModelObject model;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
private void readFile()
if (model == null)
Gson gson = new Gson();
final String helpItem = “my _file.json”;
InputStream stream = null;
try
stream = getResources().getAssets().open(helpItem);
Reader reader = new InputStreamReader(stream);
model = gson.fromJson(reader, ModelObjects.class);
reader.close();
stream.close();
catch (IOException e)
Timber.w(e);
finally
fileclose();
This is my code using this code i am reading file from assets folder and parse to Model object but i want to read this file in back ground thread instead of Main thread so and get data main thread please suggest me how to implement read file in background thread and get value in Main thread.
add a comment |
private ModelObject model;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
private void readFile()
if (model == null)
Gson gson = new Gson();
final String helpItem = “my _file.json”;
InputStream stream = null;
try
stream = getResources().getAssets().open(helpItem);
Reader reader = new InputStreamReader(stream);
model = gson.fromJson(reader, ModelObjects.class);
reader.close();
stream.close();
catch (IOException e)
Timber.w(e);
finally
fileclose();
This is my code using this code i am reading file from assets folder and parse to Model object but i want to read this file in back ground thread instead of Main thread so and get data main thread please suggest me how to implement read file in background thread and get value in Main thread.
From memory, but something likeObservable.fromCallable(() -> readFile()).subscribeOn(Schedulers.io()).subscribe()should do it
– Mauro Curbelo
Mar 7 at 2:59
add a comment |
private ModelObject model;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
private void readFile()
if (model == null)
Gson gson = new Gson();
final String helpItem = “my _file.json”;
InputStream stream = null;
try
stream = getResources().getAssets().open(helpItem);
Reader reader = new InputStreamReader(stream);
model = gson.fromJson(reader, ModelObjects.class);
reader.close();
stream.close();
catch (IOException e)
Timber.w(e);
finally
fileclose();
This is my code using this code i am reading file from assets folder and parse to Model object but i want to read this file in back ground thread instead of Main thread so and get data main thread please suggest me how to implement read file in background thread and get value in Main thread.
private ModelObject model;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
private void readFile()
if (model == null)
Gson gson = new Gson();
final String helpItem = “my _file.json”;
InputStream stream = null;
try
stream = getResources().getAssets().open(helpItem);
Reader reader = new InputStreamReader(stream);
model = gson.fromJson(reader, ModelObjects.class);
reader.close();
stream.close();
catch (IOException e)
Timber.w(e);
finally
fileclose();
This is my code using this code i am reading file from assets folder and parse to Model object but i want to read this file in back ground thread instead of Main thread so and get data main thread please suggest me how to implement read file in background thread and get value in Main thread.
asked Mar 7 at 2:46
MARSHMARSH
357
357
From memory, but something likeObservable.fromCallable(() -> readFile()).subscribeOn(Schedulers.io()).subscribe()should do it
– Mauro Curbelo
Mar 7 at 2:59
add a comment |
From memory, but something likeObservable.fromCallable(() -> readFile()).subscribeOn(Schedulers.io()).subscribe()should do it
– Mauro Curbelo
Mar 7 at 2:59
From memory, but something like
Observable.fromCallable(() -> readFile()).subscribeOn(Schedulers.io()).subscribe() should do it– Mauro Curbelo
Mar 7 at 2:59
From memory, but something like
Observable.fromCallable(() -> readFile()).subscribeOn(Schedulers.io()).subscribe() should do it– Mauro Curbelo
Mar 7 at 2:59
add a comment |
1 Answer
1
active
oldest
votes
You can do this with Observable like this:
Observable.fromCallable(new Callable<ModelObject>()
@Override public ModelObject call() throws Exception
return readFile(); // Make readFile so that it returns ModelObject
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ModelObject>()
@Override public void onSubscribe(Disposable d)
@Override public void onNext(ModelObject o)
@Override public void onError(Throwable e)
@Override public void onComplete()
);
So this we have to keep in OnCreate method? @Bach Vu
– MARSH
Mar 7 at 3:03
Anywhere you want
– Bach Vu
Mar 7 at 3:04
this are two method or Single method @Bach Vu can you please suggest me because i am new in RX
– MARSH
Mar 7 at 3:05
It's a single call, creating anObservable, then subscibe to it iniothread and listen to it onmainThread
– Bach Vu
Mar 7 at 3:06
Also, I suggest you reading this blog.mindorks.com/… for knowing when to use which types ofObservable
– Bach Vu
Mar 7 at 3:09
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55035267%2fhow-to-read-file-from-asset-folder-in-android-using-rx-in-background-thread%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do this with Observable like this:
Observable.fromCallable(new Callable<ModelObject>()
@Override public ModelObject call() throws Exception
return readFile(); // Make readFile so that it returns ModelObject
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ModelObject>()
@Override public void onSubscribe(Disposable d)
@Override public void onNext(ModelObject o)
@Override public void onError(Throwable e)
@Override public void onComplete()
);
So this we have to keep in OnCreate method? @Bach Vu
– MARSH
Mar 7 at 3:03
Anywhere you want
– Bach Vu
Mar 7 at 3:04
this are two method or Single method @Bach Vu can you please suggest me because i am new in RX
– MARSH
Mar 7 at 3:05
It's a single call, creating anObservable, then subscibe to it iniothread and listen to it onmainThread
– Bach Vu
Mar 7 at 3:06
Also, I suggest you reading this blog.mindorks.com/… for knowing when to use which types ofObservable
– Bach Vu
Mar 7 at 3:09
|
show 3 more comments
You can do this with Observable like this:
Observable.fromCallable(new Callable<ModelObject>()
@Override public ModelObject call() throws Exception
return readFile(); // Make readFile so that it returns ModelObject
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ModelObject>()
@Override public void onSubscribe(Disposable d)
@Override public void onNext(ModelObject o)
@Override public void onError(Throwable e)
@Override public void onComplete()
);
So this we have to keep in OnCreate method? @Bach Vu
– MARSH
Mar 7 at 3:03
Anywhere you want
– Bach Vu
Mar 7 at 3:04
this are two method or Single method @Bach Vu can you please suggest me because i am new in RX
– MARSH
Mar 7 at 3:05
It's a single call, creating anObservable, then subscibe to it iniothread and listen to it onmainThread
– Bach Vu
Mar 7 at 3:06
Also, I suggest you reading this blog.mindorks.com/… for knowing when to use which types ofObservable
– Bach Vu
Mar 7 at 3:09
|
show 3 more comments
You can do this with Observable like this:
Observable.fromCallable(new Callable<ModelObject>()
@Override public ModelObject call() throws Exception
return readFile(); // Make readFile so that it returns ModelObject
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ModelObject>()
@Override public void onSubscribe(Disposable d)
@Override public void onNext(ModelObject o)
@Override public void onError(Throwable e)
@Override public void onComplete()
);
You can do this with Observable like this:
Observable.fromCallable(new Callable<ModelObject>()
@Override public ModelObject call() throws Exception
return readFile(); // Make readFile so that it returns ModelObject
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ModelObject>()
@Override public void onSubscribe(Disposable d)
@Override public void onNext(ModelObject o)
@Override public void onError(Throwable e)
@Override public void onComplete()
);
edited Mar 7 at 3:37
answered Mar 7 at 3:00
Bach VuBach Vu
870513
870513
So this we have to keep in OnCreate method? @Bach Vu
– MARSH
Mar 7 at 3:03
Anywhere you want
– Bach Vu
Mar 7 at 3:04
this are two method or Single method @Bach Vu can you please suggest me because i am new in RX
– MARSH
Mar 7 at 3:05
It's a single call, creating anObservable, then subscibe to it iniothread and listen to it onmainThread
– Bach Vu
Mar 7 at 3:06
Also, I suggest you reading this blog.mindorks.com/… for knowing when to use which types ofObservable
– Bach Vu
Mar 7 at 3:09
|
show 3 more comments
So this we have to keep in OnCreate method? @Bach Vu
– MARSH
Mar 7 at 3:03
Anywhere you want
– Bach Vu
Mar 7 at 3:04
this are two method or Single method @Bach Vu can you please suggest me because i am new in RX
– MARSH
Mar 7 at 3:05
It's a single call, creating anObservable, then subscibe to it iniothread and listen to it onmainThread
– Bach Vu
Mar 7 at 3:06
Also, I suggest you reading this blog.mindorks.com/… for knowing when to use which types ofObservable
– Bach Vu
Mar 7 at 3:09
So this we have to keep in OnCreate method? @Bach Vu
– MARSH
Mar 7 at 3:03
So this we have to keep in OnCreate method? @Bach Vu
– MARSH
Mar 7 at 3:03
Anywhere you want
– Bach Vu
Mar 7 at 3:04
Anywhere you want
– Bach Vu
Mar 7 at 3:04
this are two method or Single method @Bach Vu can you please suggest me because i am new in RX
– MARSH
Mar 7 at 3:05
this are two method or Single method @Bach Vu can you please suggest me because i am new in RX
– MARSH
Mar 7 at 3:05
It's a single call, creating an
Observable, then subscibe to it in io thread and listen to it on mainThread– Bach Vu
Mar 7 at 3:06
It's a single call, creating an
Observable, then subscibe to it in io thread and listen to it on mainThread– Bach Vu
Mar 7 at 3:06
Also, I suggest you reading this blog.mindorks.com/… for knowing when to use which types of
Observable– Bach Vu
Mar 7 at 3:09
Also, I suggest you reading this blog.mindorks.com/… for knowing when to use which types of
Observable– Bach Vu
Mar 7 at 3:09
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55035267%2fhow-to-read-file-from-asset-folder-in-android-using-rx-in-background-thread%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
From memory, but something like
Observable.fromCallable(() -> readFile()).subscribeOn(Schedulers.io()).subscribe()should do it– Mauro Curbelo
Mar 7 at 2:59