facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
118.35k stars 24.25k forks source link

[Persistence] React Native & Model Layer #1892

Closed yelled3 closed 8 years ago

yelled3 commented 9 years ago

I was working on my RN iOS app and started thinking on how to work with Models;

Now, I realise that React (& RN) are the View in MVC and doesn't deal with the Model layer. More over, I know that this issue is somewhat related to issues Relay is trying to solve.

That being said, I want add persistence to my flux RN app. Let's also assume the AsyncStorage (http://facebook.github.io/react-native/docs/asyncstorage.html) is too simplistic for my needs. (I'm also unsure how performant it is on large datasets)

an obvious solution would be to use CoreData or Realm and defining my models in Native objC/Swift.

now I'm faced with 2 options:

I was wondering if there's another option I'm missing here? and also which of the above options would you generally recommend?

chirag04 commented 9 years ago

Subscribing. Would love to know best practices around this. I think you bind rocksdb to AsyncStorage. Not sure how to do that though.

Refer: https://github.com/facebook/react-native/issues/582

nicklockwood commented 9 years ago

Core Data would probably be a poor choice, as it would involve writing a lot of glue code for each and every model. If you want to go this route, I'd suggest finding something that uses JSON or plain old key/value strings as the interchange format.

Do you have any specific concerns about the performance of AsyncStorage though, or just a general feeling that it won't perform well?

It should cope well with a reasonable number of small or large objects. It may struggle with a very large number of small objects because they would all be stored in a single file. I'd suggest benchmarking it with a plausible dataset though - just generate some random data that's representative of the number and size of objects you'll be dealing with, and see how it performs - then if you have any bottlenecks, we can look at optimizing it.

nicklockwood commented 9 years ago

@chirag04 We have an implementation of AsyncStorage that uses RocksDB internally. It's not open source yet, but I guess it could be if that's something that would be useful.

chirag04 commented 9 years ago

@nicklockwood We are using asyncstorage for now as the dataset is small. Performance is satisfactory.

Our dataset is going to increase soon. I don't have performance number but asyncStorage maybe a problem that time?

Regardless, RocksDB implementation for AsyncStorage can definitely be helpful to some. Would love to see it open sourced in future.

yelled3 commented 9 years ago

@chirag04

Regardless, RocksDB implementation for AsyncStorage can definitely be helpful to some. Would love to see it open sourced in future.

:+1:

nicklockwood commented 9 years ago

I'll look into it. Our current implementation is coupled to some internal stuff, but it should be fairly easy to unpick.

yelled3 commented 9 years ago

@nicklockwood

Do you have any specific concerns about the performance of AsyncStorage though, or just a general feeling that it won't perform well?

I would like my data layer to more then a key-value storage system. I need to be able to run complex queries on a large relational dataset - correct me if I'm wrong, but AsyncStorage built for this.

nicklockwood commented 9 years ago

@yelled3 it sounds like you probably need something more like an SQLite wrapper in that case. We don't currently have an internal need for something like that, so you might need to see if someone in the community can built (or already has built) a wrapper.

brentvatne commented 9 years ago

This seems like a good place to look: https://github.com/almost/react-native-sqlite

chirag04 commented 8 years ago

@nicklockwood @vjeux We are kindof hitting the wall now where we have to decide on one long term db solution.

Here is what all we tried: 1) store everything in js files and require them. Cons: not scalable, bundle is getting crazy. initial load time increases with more data. 2) realm/coredata. Cons: confusing, relational model/ needs lot of glue code with rn 3) Sqlite(currently in production). Cons: relational model.

Possible options from here: 1) LevelDB 2) RocksDB 3) Couchbase-lite (wrapper on top of sqlite)

I think AsyncStorage with rocksDB could help solve a lot of issues here for a lot of people. What do you guys think about releasing the rockdb binding for Asyncstorage.

sahrens commented 8 years ago

We actually use RocksDB to back AsyncStorage in the AdsManager App on iOS and an old task to make a cocoapod for it that's been elanced:

https://www.elance.com/j/create-cocopod-ios-of-rocksdb/72087855/

Would be awesome if you could make a complete module :)

chirag04 commented 8 years ago

Awesome. I can make a complete module if you can share the rocksdb implementation that backs AsyncStorage in the AdsManager.

sahrens commented 8 years ago

Here's our internal module with the internal stuff stripped out:

https://gist.github.com/sahrens/ae3ad0889c608ecd51aa

It won't compile of course, but should get you started.

chirag04 commented 8 years ago

w00t! You guys are awesome. I definitely have a great start here. I will get to this over the weekend.

mvayngrib commented 8 years ago

+1 for rocksdb backed asyncstorage. Alternately, would love to see leveldown adapted. There's leveldown-mobile, which supposedly works with JXCore, how hard would it be to adapt it for RN?

jaygarcia commented 8 years ago

How is the relational model a con?

chirag04 commented 8 years ago

I tried to do this but i'm stuck. Here is the blocker issue on rocksdb: https://github.com/facebook/rocksdb/issues/729

chirag04 commented 8 years ago

@sahrens finally got rocksdb as backend for asyncstorage working..Thanks for sharing the code.

Heres the link to setup and all the relevant code: https://gist.github.com/chirag04/6dbe96a039d0bdfd31ec

I'm not sure if anyone needs it as a module.

mvayngrib commented 8 years ago

@chirag04 would be great to have it as a module

chirag04 commented 8 years ago

@sahrens I might be asking too much but any chance for android implementation as well?

@mvayngrib Ok. I will put together a module soon.

mvayngrib commented 8 years ago

@chirag04 awesome, thanks!

mvayngrib commented 8 years ago

@chirag04 any progress on the rocksdb backed asyncstorage module?

chirag04 commented 8 years ago

@mvayngrib Sorry, too busy with our apps v1.3 release. The gist i gave should be all you would need. Feel free to use the gist and publish a module. Happy to help if you have a blocker.

salbito commented 8 years ago

So I took a look through the gist and was able to compile rocksdb into my project. Now it seemed to be working fine up until I restarted the application which led to a "No locks available" Error.

@mvayngrib Did you get further with the store and have an example you could post up?

@chirag04 Is there a more up to date version of those native modules that might include closing the database connection so it would rid me of this "No locks available" issue?

mvayngrib commented 8 years ago

@salbito haven't tried this yet. I was running xcode 6.4 at the time, which choked when i fed it rocksdb, and haven't gotten back to it yet. Btw, does it work in the simulator? @chirag04 mentioned it only worked on the device but i forget why

salbito commented 8 years ago

@mvayngrib @chirag04

I kept plugging away it and it seems to be working more reliably now. The locking issue has seemingly disappeared as well. It appears the locking was due to some references I kept around for debugging purposes within callbacks. I'm going to keep plugging away at it and report back here.

Now one thing did change as far as instructions went on the gist. I actually had to set Enable Bitcode to false in order to get it to build. After that things appear to work swimmingly.

mvayngrib commented 8 years ago

@salbito awesome! Can you publish it as a module?

chirag04 commented 8 years ago

@salbito awesome :+1:

christopherdro commented 8 years ago

@facebook-github-bot answered

facebook-github-bot commented 8 years ago

Closing this issue as @christopherdro says the question asked has been answered. Please help us by asking questions on StackOverflow. StackOverflow is amazing for Q&A: it has a reputation system, voting, the ability to mark a question as answered. Because of the reputation system it is likely the community will see and answer your question there. This also helps us use the GitHub bug tracker for bugs only.

mvayngrib commented 7 years ago

@chirag04 @salbito a bit late, but published it as a module react-native-async-storage-rocks

chirag04 commented 7 years ago

Thanks for publishing. 👍

cbfranca commented 6 years ago

@yelled3 , how did you solve your situation ?