Comments (47)
I have an idea of what to do just need some time
from nativescript-couchbase-plugin.
Good idea itβs simple to do this first you need to create your method an a callback so you can use that to make it into a promise on the js side
from nativescript-couchbase-plugin.
@jerbob92 are you doing it ? because I started something
from nativescript-couchbase-plugin.
@triniwiz, yeah you can see it in my fork. Curious what approach you are taking though.
from nativescript-couchbase-plugin.
from nativescript-couchbase-plugin.
Like I said: curious what approach you are taking though. I'm following the NativeScript core approach.
from nativescript-couchbase-plugin.
It's ok my i started with was passing the built object e.g Select
to the java class then let it execute there then try using Gson
to serialize to a string pass that back to the listener then parse it in the js world
from nativescript-couchbase-plugin.
public class FancyCouchbase {
private static ExecutorService executorService = Executors.newSingleThreadExecutor();
private static Gson gson = new Gson();
public interface QueryListener {
void onSuccess(String results);
void onError(String error);
}
public void queryInBackground(final Select select, final QueryListener listener) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
String json = gson.toJson(select.execute().allResults());
listener.onSuccess(json);
} catch (CouchbaseLiteException e) {
listener.onError(e.getLocalizedMessage());
}
}
});
}
}
from nativescript-couchbase-plugin.
That's only for the query though, how would you make the other methods threaded? The code in my branch already works for Android, only the batch method is hard to make threaded when stuff in the runnable are made out of Promises.
from nativescript-couchbase-plugin.
Does the ios methods need to be called on another thread or is it fine in your tests ?
from nativescript-couchbase-plugin.
I didn't get to iOS yet, wanted to have Android stable first. But my plan is to make both of them threaded.
from nativescript-couchbase-plugin.
For the inBatch I might have an idea
from nativescript-couchbase-plugin.
Cool! I just noticed I didnt commit everything yet. Fork is updated now.
from nativescript-couchbase-plugin.
Something like the following should be fine
public void inBatch(final Runnable runnable) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
database.inBatch(runnable);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
}
});
}
from nativescript-couchbase-plugin.
That will not work. My code for the inBatch does work like this currently. But since the calls inside the runnable now also use promises, the run() method on the runnable immediately returns, while the actuall calls within those promises didn't run yet. I have to find some way to make the runnable.run() return after those promises have completed. Mixing Java and JavaScript can have its problems π
from nativescript-couchbase-plugin.
I have figured out a solution for the batch problem in my fork. I first tried an approach by using a custom class that implemented the Runnable interface that would just use Thread.pause(50) while the Promise was still running. Technically, this worked, but the native Couchbase inBatch/save/delete methods locks the db to it's current thread, and since the update/create/delete calls in the plugin are now also threaded, and run in a different thread, they will wait for a lock forever since the inBatch already has the lock. And the inBatch will only finish when all the Promises are finished, so that's something you will never get out of.
Second approach was creating new methods for the update/create/delete that will return a BatchAction object, it contains the native document and the action that should be done to it. The inBatch now wants an array of BatchAction, it will create native Java BatchAction objects in a Java list and send that to the native plugin. It will then loop through the BatchAction list in a seperate runnable in the native Couchbase inBatch method with all the actions that are in the BatchAction. This will make sure all actions are executed on the same thread. It's also a lot faster because it prevents going back and forth between native and JS.
Sadly, I'm still having a lot of UI hangs. So going to debug some more now. At least the DB stuff is threaded now. I think the serializing is also quite heavy on the CPU.
from nativescript-couchbase-plugin.
for serializing have you looked at gson ?
from nativescript-couchbase-plugin.
How would gson fit into this? Serialize it in the thread and then JSON.parse in JS?
from nativescript-couchbase-plugin.
Yes :) the performance for JSON.parse
should not be bad
from nativescript-couchbase-plugin.
I'm not sure if the slowness is actually coming from the serialize, I measured it and it's not really that slow.
from nativescript-couchbase-plugin.
I figured out some of my JSON objects were very large due to some issue on the API which caused some slowness in the JSON.Parse in NS.
Currently our Android threaded version in working great! I just pushed an update to my fork that's feature complete and everything is working. I also figured out that there is some issue in your changeListener implementation, only one worked, so I fixed that too and made the implementation lighter on NS. Since the source doesn't include your Android lib, I added it to mine, which is now included in the repo.
The iOS implementation is on hold right now, but if anyone else would like to pick to up, it would be great.
from nativescript-couchbase-plugin.
I think we could leave the older api an append lets say async
to new methods to help maintain some compatibility with the old couchbase plugin
from nativescript-couchbase-plugin.
It might be better to release a new major version. But if anyone likes to put time in creating an async version, sure.
from nativescript-couchbase-plugin.
The same code you did can be used since itβs all promises
from nativescript-couchbase-plugin.
I'm not certain you can just put async somewhere and it runs promises in sync anywhere.
from nativescript-couchbase-plugin.
@triniwiz We finished the iOS threaded version. Are you interested in merging it all together to create a 2.0 version?
from nativescript-couchbase-plugin.
Pr away
from nativescript-couchbase-plugin.
@triniwiz sure :)
But I have some cleaning up to do and want to discuss some things:
- Where does the native code live? In the same repo?
- Do we push the native libraries somewhere? (Maven/CocoaPods)
- If we do, who will be the owner of those libraries?
- I changed everything to Promises, because it makes more sense when everything is non-blocking. Do you agree?
from nativescript-couchbase-plugin.
Native code can live in the platforms directory for both platforms i never tested for swift code
from nativescript-couchbase-plugin.
We can release it as v2
from nativescript-couchbase-plugin.
I don't think you can directly add the native libs into your platform folders and have it work.
Android need's an aar right? For iOS we can probably add a Podfile that points to a folder in stead of a version. I will try next week!
from nativescript-couchbase-plugin.
You can do this for android in the platforms directory and maybe even this for ios
from nativescript-couchbase-plugin.
I think that's for NativeScript projects, not plugins.
from nativescript-couchbase-plugin.
So this is wrong ?
from nativescript-couchbase-plugin.
@jerbob92 What is the status with your background thread idea? I have seen your changes and looks good.
Do you intend to create a pull request for this plugin? It would be nice and helpful ;)
Thanks ;)
from nativescript-couchbase-plugin.
@alexandruantonica We are using our Android fork in production, it's working great :) We already created an iOS version but are not using that one yet due to stability problems.
However, we do intend to create a pull request. If someone else wants to pick that up, here's the fork that contains the Android work: https://github.com/jerbob92/nativescript-couchbase-plugin/commits/feature/threading
from nativescript-couchbase-plugin.
from nativescript-couchbase-plugin.
@alexandruantonica I can push our work on the iOS version, but you will need to do some testing/fixing on your own to get it production ready. Let me know and I will push the work :)
from nativescript-couchbase-plugin.
from nativescript-couchbase-plugin.
I just pushed it :)
The native library is in https://github.com/jerbob92/nativescript-couchbase-plugin/tree/feature/threading/native-libraries/ios
The bridging code is in https://github.com/jerbob92/nativescript-couchbase-plugin/blob/feature/threading/src/couchbase-plugin.ios.ts
This will probably not work automatically, we used a private Podspec repo to download the library. But as @triniwiz said, this might not be necessary.
from nativescript-couchbase-plugin.
@alexandruantonica did you manage to get this to work on both platforms (Android and iOS)?
from nativescript-couchbase-plugin.
@Pkurto No, unfortunately, I was busy and I wasn't able to take a look. If you manage to take a look and do something, let me know.
from nativescript-couchbase-plugin.
Hi @jerob92,
Can you create a pull request for this plugin to include the android background thread?
It will be amazing to use this feature :)
from nativescript-couchbase-plugin.
@Pkurto no, my fork changes a lot of methods, so it's not backwards compatible. It's also not really useful to create a pull request when the iOS version is not finished/stable yet. If you do want to use it you can use my fork.
from nativescript-couchbase-plugin.
@jerbob92 thank you for the quick response and effort to develop this feature. I think this is one of the most important enhancements for this plugin.
I was only suggesting a PR only because I was thinking that the Android code could be added without the iOS alterations.
Also, I am using this plugin in a Project and @triniwiz already corrected an issue related with case insensitive search. If I use your fork I will lose that correction :(
There is any documentation that can help If I manage to get some time to see if I can help with iOS version?
from nativescript-couchbase-plugin.
@Pkurto That's possible but then in my opinion the plugin is not complete. If you want recent fixes in my fork you can create your own fork of my fork and then merge the commits from @triniwiz.
There's not really any documentation for the iOS version, and we are also not planning on using this plugin anymore so we're not going to put more time into it.
from nativescript-couchbase-plugin.
A simple solution is to just call the database queries on a worker thread.
https://docs.nativescript.org/core-concepts/multithreading-model
from nativescript-couchbase-plugin.
Related Issues (20)
- Is posible create pull and push by documentId ?
- QueryArrayOperator: what about it?
- GROUP BY
- ERROR NSErrorWrapper: unable to open database file
- QUERY: WHERE HOT 2
- Storage of App on devices increase even deleting data HOT 2
- Support for CBL 2.7 HOT 3
- deleteDocument missing ConcurrencyMode Param HOT 1
- Can't find variable: CBLDatabase Error
- Couchbase Lite Exception -> Can't delete db file while other connections are open HOT 7
- createView is not a function HOT 1
- how to get count of items from document HOT 4
- Need query to get data using offset and limit HOT 2
- runtime visualise data from database HOT 1
- Migrate plugin to use new @nativescript/core imports HOT 2
- Changing to @nativescript/core HOT 1
- Enquiry: Secure / Encrypt Couchbase DB with a Password HOT 1
- minSdk version error
- Data on syncing HOT 1
- [Question] Prebuild Database
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nativescript-couchbase-plugin.