Realm backed by several files? - realm

Is it possible to manage data stored amongst several files?
Let's say I have several files data1.realm, data2.realm, data3.realm, etc. containing objects with the same model. Is it possible to get a unique RLMRealm instance that will access the datas of all these files?
If not, what is the best way to handle this situation? Migration?

It's definitely possible to manage data stored amongst separate Realms, but it wouldn't be automatic. You would need to manage access to this data in your own app's logic.
RLMRealm instances themselves represent a single file on disk and cannot be dynamically created to reference combinations of other Realms. Once an RLMObject has been added to a parent RLMRealm, it cannot be moved/backed to another RLMRealm representing a different file.
It most likely depends on your specific use-cases, but the simplest solution would be to simply query for your objects in separate RLMRealm instances for each file, and placing the resulting RLMResults objects from each one in an NSArray.
While data can't be directly shared between Realms, you could use globally unique primary keys (For example NSUUID) to indicate relationships between objects in different Realms.
If you need, it's also possible to create Realmless copies of RLMObjects if you do end up wanting to move objects between Realms:
Dog *savedDog = [[Dog allObjects] firstObject];
Dog *copiedDog = [[Dog alloc] initWithValue:savedDog];

Related

Is it possible to 'ATTACH' a SQLdatabase in a strongly typed dataset in .net

I am using a typed dataset (Devart.Data.SQLite) with SQLite in .net.
I have two separate databases, Master Parts List (MPL) and a Project DB (ProjDB). There could be many ProjDB's but there needs to be only 1 MPL, shared by everyone. The ProjDB will reference an PartID located in the MPL. I want to 'ATTACH' the MPL to my ProjDB in order to draw information from the MPL. I know the recommended course to have everything in the same database for data integrity, however the parts in the MPL will never be deleted. There will only be additions and slight text modifications. Also the MPL is large and I don't want to replicate it every project.
I can add two separate connections in the Datadesigner and created a relation allowing me to create a Master/detail (in separate grids) across the two databases. This fulfills most requirements, but I need to show a row with projDB and MPL data in the same row and the datadesigner will not let me do this.
I have only been able to 'ATTACH' using a 'ExecuteNonQuery' in an untyped dataset.
Should I ditch typed datasets and switch over to untyped?

Best approach to having multiple users in one app

This is mobile app which can have different kind of users. I'm using realm only for the offline storage. Say I have two users A and B and a have a List Class. This class wont ever be shared, so different data for each user. How would i go in designing the schema? Considering versioning and migration.
A. Add a primary key for the List and assign it differently to user A and B.
B. Use two different realms
There is no one good way of defining your Realm schema and the solution to choose completely depends on the exact scenario.
If you want your users data to be completely independent of each other and you will never need to use a single query to retrieve both users data or to access some common data, then using separate Realm instances for each use seems like a good approach. It provides complete separation between your users data.
However, if your users might have some shared data or if you might end up making some statistics about all of your users even though their data is independent, using a single Realm instance is the way to go. In this case you should just create a one-to-many relationship between each of your users and whatever objects you want to store in your lists like this:
class User:Object {
let stuff = List<Stuff>()
}

Core Data sqlite store single table due to inheritance drawbacks?

I have a Core Data model with something like 20 entities. I want all entities to have common attributes. For example, all of them have a creation date attribute.
I therefore introduced an common entity containing all the common attributes, and all the other entities inherit from this common entity.
This is fine and works well, but then, all entities end up in one single SQLite table (which is rather logical).
I was wondering if there was any clear drawback to this ?
For example, when going in real life with 1000+ objects of each entity, would the (single) table become so huge that terrible performance problems could happen ?
This question has been asked before:
Core Data entity inheritance --> limitations?
Core data performances: when all entities inherit from the same parent entity
Core Data inheritance vs no inheritance
Also keep in mind that when you want to check the SQLite file for debugging purposes, seperate tables are easier to examine.
I would use a common NSManagedObject subclass instead of a parent entity.
Don't worry about this. From Core Data documentation:
https://developer.apple.com/library/tvos/documentation/Cocoa/Conceptual/CoreData/Performance.html
... The SQLite store can scale to terabyte-sized databases with billions of rows, tables, and columns. Unless your entities themselves have very large attributes or large numbers of properties, 10,000 objects is considered a fairly small size for a data set.
What is way more important is that if you are doing any heavy operations, like fetching a lot of objects, or parsing objects based on some JSON from a webservice, you do this not on the mainthread. This is not very hard to do, look into parent/child managedobjectcontexts and how they can be used with managedcontextobjects with a private / main queue concurrencytype. Many good blog posts about this subject exist all over the interwebs.
I've been working on a project with one base entity for around 20 subentities and easily overall 50k instances for over 2 years now. We've never had performance problems with selects, inserts or updates.
The keys to using Core Data inheritance with large data sets are
optimized fetch requests (tune predicate, exclude irrelevant properties, prefetch relationships, omit subentities, set fetchLimit, use dictionary result type or count-requests if sufficient etc.)
batch saves (meaning not saving the MOC after every insert etc.)
setting up proper indices (they can speed up selects a looot)
structuring your UI appropriately so you won't have to load and display many thousand objects in one viewController
We do not even use parent/child managedObjectContexts or private queues (which introduce a lot of extra complexity on their own) when importing JSON, as our data model and mapping code is so highly optimized, that the UI doesn't even flicker or hang considerably when importing a few thousand objects.

Temporary Storing data entered in steps in class in asp.net

how can i store different data enetered using multiple steps?
Its 4-5 step procedure, user can go through each step & at the final submission i want to store it in database.
I want to create class to store this data & store the data in object of that class & use it from session.
I just want to know what type should i use to store multiple data.
i.e. array,ienumrable,list,etc..
Any suggestion or example to implement class like this.
Thanks
Pragnesh
List is generally most useful. If the data items map to specific fields though you could just make them properties in the class (you could use reflection if you wanted to make the code generic).
Alternatively for a wizard you could use the built in wizard control, which stores all the field data in viewstate. This is generally better than storing in session - for example if the user opened two browsers it would get confused.
Depends on how many data you need to maintain between steps and how many visitors will use the steps concurrently - server resources are limited.
The ways to use:
Session state (may be lost, but uses server resources)
Viewstate (can't be lost, but uses traffic)
Regarding types to use - use types that are not complex and are compact after serializing, more primitive. Test it.

n-tier design, lookup tables, and custom entities

In an n-tiered application where you are using custom entities, how do you find yourself handling data needed from lookup tables? Do you create entities for each of these lookup tables or employ some other strategy?
For example. I have a "Ratings" lookup table that will be used to populate a dropdownlist. Would you create a ratings object with a ratingid and rating property and pass that to your UI or is there a more efficient way to go about it?
Appreciate your thoughts.
I'd suggest that the solution will depend on how often the lookup data changes, whether or not it needs to be editable, and whether or not you're enforcing referential integrity at the database. I think it makes the schema more understandable if you put each lookup type into a separate table.
I generally don't create entities for each lookup table, but instead will load most of the common lookups into structures that are easily re-used by the application - for an asp.net app, for example, I'll create hashtables or ordered dictionaries which can easily be bound to most web controls.
And, horror of horrors, I sometimes create a singleton to manage access to all these lookups, which can be stored as static vars or in the cache, depending on requirements.
We seperate out the different look up types into different objects. It seems to be a little more work up front, but it provides us the ability to make changes to each individual object when we need to, such as an addition of additional information to an object.

Resources