React Native and Realm - Architecture - react-native

So I'm building a React Native application where I'll need to have my data stored on the device offline, only syncing the data in the morning and evening.
The solution I've is Realm. My question is about the structure of the application. As far as I can tell using a Realm database removes the need to have a flux architecture manage my state as this is what Realm would be doing no?
I wanted to see what people are doing in terms of structure or design patterns. Would I implement some sort of ad-hoc mvc? Or mimic redux somehow?
I am asking about the app's possible design pattern but also about the physical folder structure. The app has the potential to be quite complex so I need to plan this thing out before I start.
Thoughts?

This resource was pretty helpful for me in understanding how to mix Realm with Flux/Redux.
https://medium.com/#manggit/react-native-redux-realm-js-r3-js-a-new-mobile-development-standard-5290ec02a590
My opinion is that there are many other types of application state that can still be managed in Flux/Redux besides what might be persisted in Realm so I like this approach and it has been working for me so far.

Related

Which Js Backend Framework for React Native woudl you recommend at the moment?

Which Js Backend Framework for React Native would you recommend at the moment?
The main thing i will do you in the Backend is to Read and Write Data to the Database.
SQL Server and maybe Oracle
Meteor, Sails, Koa, Express or another one?
I've read alot about the Frameworks, but it seems everyone prefers another one.
What is your opinion
Meteor is tightly integrated with MongoDB, though you can use vlasky:mysql. But you get the idea, it's best if you were to use it as is with MongoDB. So, this leave us with Sails, Koa, Express. They're kind of similar. BTW, Sails and Koa are built on top of Express. Now, the difference between the two is Sails follows batteries included philosophy so it comes with many built-in goodies, including Water ORM which you can connect to different datasource.
All in all, it's better to not fall in the paradox of choice and actually start building your application as soon as possible. ;)

What's architecture most widely used to built large scale application in react-native?

I'm want to create a large scale mobile app in react-native, after some research and read the various articles from medium and other websites I'm total confused what architecture i'll used to make my application better or developer friendly architecture. Please help me to find the best react-native architecture to build large scale application. Thanks in advance!!
There isn't a single possibility of how you want to structure your files. There are various recommendations on how do to so, but they basically boil down to making sure to have a logical file structure, trying to group files that "work together" in a folder, and trying not to keep files that have nothing to do with each other in the same one. Use the base folder to keep global project files, and the deeper in the navigation stack, the deeper the files should be (If there is only one path through which they can be accessed). Name reducers/sagas/etc accordingly, and you should be well on your way. But most importantly, there is no correct way, only the one that makes the most sense for you. If it seems cluttered, you can always reorganize, so don't worry too much now about scaling, and focus on starting your application (But make sure it isn't a complete mess!).
for sure, there're different ways to organize your app. In our team, we worked closely with our mobile department to create an architecture for our current React Native projects.
The key point here is to split responsibilities across special components dedicated to doing the only one thing. We divided our components into several layers to achieve a better understanding of what part of business logic should go where:
Screens
Containers
CombineViews
Views
In the article, we described every step with code snippets as examples. Take a look.
The approach of arranging business logic helps to clearly know what goes where not polluting containers or simple views with state and methods. Hope it will help you in your future projects!
Clean architecture in very trendy at the moment. Maybe you can check this out : https://github.com/eduardomoroni/react-clean-architecture
Clean architecture let you separate concern of your app.

Angular 6 - Why use #ngrx/store rather than service injection

I am recently learning Angular 6 with #ngrx/store while one of the tutorial is to use #ngrx/store for state management, however I don't understand the benefit of using #ngrx/store behind the scene.
For example, for a simple login and signup action, previously by using the service(Let's call it AuthService) we might use it to call the backend api, store "userInfo" or "token" in the AuthService, redirect user to "HOME" page and we can inject AuthService in any component where we need to get the userInfo by using DI, which simply that one file AuthService handles everything.
Now if we are using #ngrx/store, we need to define the Action/State/Reducer/Effects/Selector which probably need to write in 4 or 5 files to handle above action or event, then sometimes still we need to call backend api using service, which seems much much more complicated and redundant...
In some other scenario, I even see some page uses #ngrx/store to store the object or list of object like grid data., is that for some kind of in-memory store usage?
So back to the question, why are we using #ngrx/store over service registration store here in Angular project? I know it's for "STATE MANAGEMENT" usage, but what exactly is the "STATE MANAGEMENT"? Is that something like transaction log and When do we need it? Why would we manage it on the front end? Please feel free to share your suggestion or experience in the #ngrx/store area!
I think you should read those two posts about Ngrx store:
Angular Service Layers: Redux, RxJs and Ngrx Store - When to Use a Store And Why?
Ngrx Store - An Architecture Guide
If the first one explains the main issues solved by Ngrx Store, it also quote this statement from the React How-To "that seems to apply equally to original Flux, Redux, Ngrx Store or any store solution in general":
You’ll know when you need Flux. If you aren’t sure if you need it, you don’t need it.
To me Ngrx store solves multiple issues. For example when you have to deal with observables and when responsability for some observable data is shared between different components. In this case store actions and reducer ensure that data modifications will always be performed "the right way".
It also provides a reliable solution for http requests caching. You will be able to store the requests and their responses, so that you could verify that the request you're making has not a stored response yet.
The second post is about what made such solutions appear in the React world with Facebook's unread message counter issue.
Concerning your solution of storing non-obvervable data in services. It works fine when you're dealing with constant data. But when several components will have to update this data you will probably encounter change detection issues and improper update issues, that you could solve with:
observer pattern with private Subject public Observable and next function
Ngrx Store
I'm almost only reading about the benefits of Ngrx and other Redux like store libraries, while the (in my opinion) costly tradeoffs seem to be brushed off with far too much ease. This is often the only reason that I see given: "The only reason not to use Ngrx is if your app is small and simple". This (I would say) is simply incomplete reasoning and not good enough.
Here are my complaints about Ngrx:
You have logic split out into several different files, which makes the code hard to read and understand. This goes against basic code cohesion and locality principles. Having to jump around to different places to read how an operation is performed is mentally taxing and can lead to cognitive overload and exhaustion.
With Ngrx you have to write a lot more code, which increases the chances of bugs. More code -> more places for bugs to appear.
An Ngrx store can become a dumping ground for all things, with no rhyme or reason. It can become a global hodge podge of stuff that no one can get a coherent overview of. It can grow and grow until no one understands it any more.
I've seen a lot of unnecessary deep object cloning in Ngrx apps, which has caused real performance issues. A particular app I was assigned to work on was taking 40 ms to persist data in the store because of deep cloning of a huge store object. This is over two lost render frames if you are trying to hit a smooth 60 fps. Every interaction felt janky because of it.
Most things that Ngrx does can be done much simpler using a basic service/facade pattern that expose observables from rxjs subjects.
Just put methods on services/facades that return observables - such a method replaces the reducer, store, and selector from Ngrx. And then put other methods on the service/facade to trigger data to be pushed on these observables - these methods replace your actions and effects from Ngrx. So instead of reducers+stores+selectors you have methods that return observables. Instead of actions+effects you have methods that produce data the the observables. Where the data comes from is up to you, you can fetch something or compute something, and then just call subject.next() with the data you want to push.
The rxjs knowledge you need in order to use ngrx will already cause you to be competent in using bare rxjs yourself anyways.
If you have several components that depend on some common data, then you still don't need ngrx, as the basic service/facade pattern explicitly handles this already.
If several services depend on common data between them, then you just make a common service between these services. You still don't need ngrx. It's services all the way down, just like it is components all the way down.
For me Ngrx doesn't look so good on the bottom line.
It is essentially a bloated and over engineered Enterprise™🏢👨‍💼🤮 grade Rxjs Subject, when you could have just used the good old and trusty Rxjs Subject. Listen to me kids, life is too short for unnecessary complexity. Stick to the bare necessities. The simple bare necessities. Forget about your worries and your strife.
I've been working with NgRx for over three years now. I used it on small projects, where it was handy but unnecessary and I used it in applications where it was perfect fit. And meanwhile I had a chance to work on the project which did not use it and I must say it would profit from it.
On the current project I was responsible for designing the architecture of new FE app. I was tasked to completely refactor the existing application which for the same requirements used non NgRx way and it was buggy, difficult to understand and maintain and there was no documentation. I decided to use NgRx there and I did it because of following reasons:
The application has more than one actor over the data. Server uses
the SSE to push state updates which are independent from user
actions.
At the application start we load most of available data which are
then partially updated with SSE.
Various UI elements are enabled/disabled depending on multiple
conditions which come from BE and from user decisions.
UI has multiple variations. Events from BE can change currently
visible UI elements (texts in dialogs) and even user actions might
change how UI looks and works (recurring dialog can be replaced by
snack if user clicked some button).
State of multiple UI elements must be preserved so when user leaves
the page and goes back the same content (or updated via SSE) is
visible.
As you can see the requirements does not meet the standard CRUD operations web page. Doing it the "Angular" way brought such complexity to the code that it became super hard to maintain and what's worst by the time I joined the team the last two original members were leaving without any documentation of that custom made, non NgRx solution.
Now after the year since refactoring the app to use NgRx I think I can sum up the pros and cons.
Pros:
The app is more organized. State representation is easy to read,
grouped by purpose or data origin and is simple to extend.
We got rid of many factories, facades and abstract classes which lost
their purpose. The code is lighter, and components are 'dumber', with
less hidden tricks coming from somewhere else.
Complicated state calculations are made simple using effects and
selectors and most components can be now fully functional just by
injecting the store and dispatching the actions or selecting the
needed slice of the state while handling multiple actions at once.
Because of updated app requirements we were forced to refactor the
store already and it was mostly Ctrl + C, Ctrl + V and some renaming.
Thanks to Redux Dev Tools it is easier to debug and optimize (yep
really)
This is most important - even thought our state itself is unique the
store management we are using is not. It has support, it has
documentation and it's not impossible to find solutions to some
difficult problems on the internet.
Small perk, NgRx is another technology you can put to your CV :)
Cons:
My colleagues were new to the NgRx and it took some time for them to
adapt and fully understand it.
On some occasions we introduced the issue where some actions were
dispatched multiple times and it was difficult to find the cause of
it and fix it
Our Effects are huge, that's true. They can get messy but that's what
we have pull requests for. And if this code wasn't there it would
still end up somewhere else :)
Biggest issue? Actions are differentiated by their string type. Copy
an action, forget to rename it and boom, something different is
happening than you expect, and you have no clue why.
As a conclusion I would say that in our case the NgRx was a great choice. It is demanding at first but later everything feels natural and logical. Also, when you check the requirements, you'll notice that this is a special case. I understand the voices against NgRx and in some cases I would support them but not on this project. Could we have done it using 'Angular' way? Of course, it was done this way before, but it was a mess. It was still full of boiler plate code, things happening in different places without obvious reasons and more.
Anyone who would have the chance to compare those two versions would say the NgRx version is better.
There is also a 3rd option, having data in service and using service directly in html, for instance *ngFor="let item of userService.users". So when you update userService.users in service after add or update action is automatically rendered in html, no need for any observables or events or store.
If the data in your app is used in multiple components, then some kind of service to share the data is required. There are many ways to do this.
A moderately complex app will eventually look like a front end back end structure, with the data handling done in services, exposing the data via observables to the components.
At one point you will need to write some kind of api to your data services, how to get data in and out, queries, etc. Lots of rules like immutability of the data, and well defined single paths to modify the data. Not unlike the server backend, but much quicker and responsive than the api calls.
Your api will end up looking like one of the many state management libraries that already exist. They exist to solve difficult problems. You may not need them if your app is simple.
NGRX sometimes has a lot of files and a lot of duplicate code. Currently working on a fix for this. To make generic type classes for certain NGRX state management situations that are very common inside an Angular project like pagers and object loading from back-ends

Communicate between two unrelated components

How do you handle communication between components with no common parent in react-native ?
What's the best way to communicate between two unrelated components ?
I did some research and the most recommended way seems to be using context https://reactjs.org/docs/context.html.
There are multiples way to handle a global state, one of them is the new React Context API, but one of the most famous is redux, written by Dan Abramov, who works at Facebook, inspired by flux, an old facebook library to manage global state. There is also Mobx and many others, I suggest you take a look at each one and see what fits you best, it's hard to tell you without knowing your problem. Feel free to ask any other question about one of them

Database Access from iOS App

I've an iOS app that interacts with a custom API, that in turn interacts with the DB. I was thinking to eliminate the custom API step and access the DB directly from the iOS application (MongoDB).
Now several questions emerged:
It would be a security issue to distribute login credentials (even if they're encrypted) with the app.
I guess due to flaky networks the DB could be corrupted if designed improperly.
Are there any real world examples where a database backend is directly accessed from Cocoa?
Basically it boils down to yes or no - and why.
PS: The database resides on the web, not an intranet/corporate network
There are pros and cons. I think you listed all the cons. IMO, there are no pros other than having one less layer to maintain. However, if you think this particular database will ever be accessed by anything other than the iOS app, you might as well go ahead and do the intermediate layer - you're going to need it eventually. Might as well plan for it at the outset.
I'd look at using a framework like RailsKit to take care of the work.
As a proof-of-concept, we built an app that connected directly with a Rails backend, and the syncing worked well --- except that it was a bit of a pain to make it play nice: the interface would freeze while it waited for confirmation from the DB, etc.
So we're going to use an existing, available framework to take care of that work for us, and focus our development efforts on the interface and user experience.