Grails create domain object in a different data source - grails

I want to create a domain object in different data sources based on a condition but when we instantiate a new object like new Foo().save() there is no way to set which data source it will be done.
I've also noticed that if I do new Foo().dataSource2.save() it still saves in default (unnamed) data source in addition to dataSource2. How can we instantiate object only in one data source and save only in there?
I'm using grails 2.4.3.
Edit: To make it clear. I want to dynamically pick which data source to create the user in during runtime.

It seems new Foo().dataSource2.save() will not try to save it in the unnamed data source at a later point. This solution actually works.

Related

Remove or Add an attribute to CoreData at runtime programmatically

I have to create and remove attributes based on an api response in Objective C.
For example, Now my api response contains fields "facebook", "whatsapp" and "viber". But in future the reponse can add "youtube". Based on this response, I have to remove all the attributes and values of an entity "Social", and create Four attributes now and set values.
How to do that programmatically? Because the default *.xcdatamodeld file cant help me here, right?
Note: My project is in objective C.
The data model is mutable when the app starts-- you can completely build the model in code, and not use the model editor, for example. But as soon as you load a persistent store file, you must treat the model as fixed. Any changes after loading a persistent store will cause crashes. That means any changes would have to happen before calling either loadPersistentStores(completionHandler:) or addPersistentStore(with:completionHandler:).
Alexander's suggestion of optional attributes is a good one. If you need the model to be more dynamic, you would need to create a new related entity which would store the service name plus whatever information you need to save about the service. If you did this, your Social entity would have a to-many relationship to a new entity called something like Service. Service would have a string property called name that would have values like twitter, facebook, youtube, etc. It would also have whatever other attributes you need to save about the service.
You can create all 4 fields in advance and just make them optional and fill them depending on the server response. But you cannot add new attributes in runtime. Your *.xcdatamodeld file compiles into *.momd and it contains all the data to create tables in the DB since Core Data by default works with SQLite under the hood and it's a relational database management system.
To make attributes optional you should check that.
And then newly created objects contain nil as default values of object properties. So, in your case your "youtube" property of Social object will be just nil.

How to add same object type as different tables in same realm?

I'm using realm to cache certain network data coming down which is specific to the last search. I'd also like to be able to star or pin this data to save it for future usage. The data objects are exactly the same and I can't find anything in the documentation that allows me to save them in separate tables in Realm.
The easiest way to go about doing this would be to simply create a subclass of your model object with a different name (e.g., MyDataObject and its subclass MySavedDataObject). This will create a new table in the Realm database file with the same schema and will let you distinguish between the two types of objects.
You can then create a copy of a normal object as a saved object as simply as the following:
let myNewSavedObject = MySavedDataObject(value: myDataObject)
That all being said, instead of duplicating data, I would personally recommend being a bit more efficient with the existing data set. Surely simply adding an additional boolean property to the schema named something like saved would let you simply mark objects that you wish to keep without needing a whole second table. :)

Create Core Data entities dynamically during runtime

I need to be able to create new core data entities during runtime. I've written the code to create the objects programmatically, however, I can't add the entities during runtime as the model is immutable.
My problem is similar to this post, however there is no satisfactory answer: How to dyanmic create a new entity (table) via CoreData model?
The documentation regarding changing the core data model explains:
Managed object models are editable until they are used by an object
graph manager (a managed object context or a persistent store
coordinator). This allows you to create or modify them dynamically.
However, once a model is being used, it must not be changed. This is
enforced at runtime—when the object manager first fetches data using a
model, the whole of that model becomes uneditable. Any attempt to
mutate a model or any of its sub-objects after that point causes an
exception to be thrown. If you need to modify a model that is in use,
create a copy, modify the copy, and then discard the objects with the
old model.
However, I'm unclear on what exactly this is saying--that the whole core data model can't be changed once the persistent store coordinator has been used or the attributes/etc of the individual entities can't be changed.
To be clear, I do not want to change the attributes of my current entities, I simply want to add new entities. It just seems weird to me to have to use migration to add new entities.
Any thoughts?
Thanks!
The documentation is pretty clear.
Copy the model.
Apply your changes to the new copy.
Destroy your old MOC, Persistent Store Coordinator, and all objects created from those.
Apply a migration, if necessary.
Create a new Core Data Stack (MOC, PSC, etc) using your updated model.
The migration could be a sticking point, but it should be do-able.

With Breeze.js, Is There a Way to Create an Uninitialized Entity and Bind It to an "Add New" Form?

So far all of the examples that I have seen for adding a new entity would go through the following steps:
Create a bunch of "new-" variables that get bound to on screen controls.
When user wants to submit the addition, created an uninitialized new entity.
Copy "new-" variables to each member of the new entity one by one.
Push the new entity onto the manager's entity list.
Save changes.
Clear all of the "new-" variables.
This is problematic for many reasons. These "new-" variables have to be maintained in addition to the on screen controls. When server side entities change, they must be changed manually. This is time consuming and error prone.
I would like to be able to create an uninitialized new entity first, and bind it to on screen controls immediately, without using those variables with the "new-" prefix. When user wants to submit the addition, push the new entity onto the manager's entity list, and then save changes. Then bind the on screen controls immediately to a newly created uninitialized entity.
This way, we can avoid dealing with individual entity attributes in the view model, which would produce more robust code, and save a lot of time.
Right now, the metadata for creating a new entity is not available when the document becomes ready. If I download it, I have to deal with asynchronous completion before I can bind a new entity to on screen controls.
So my question is: is there a way to have the metadata downloaded with the initial HTML download so that I can create an uninitialized new entity without waiting, and bind it to on screen HTML controls immediately?
I'm not sure what means uninitialized new entity, but yes - you can create for example a new car like this:
var newCar = manager.metadataStore.getEntityType("Car").createEntity();
manager.addEntity(newCar);
Bind your controls to the newCar. To save the changes call manager.saveChanges();, to cancel call manager.rejectChanges();
To be able to work with entities breeze needs metadata. If you want, you can send metadata with the page itself. You can then use manager.importMetadata() to import it into the manager. The only problem is that you will need to write a small app that will generate the metadata string (during build) so that later it can be passed to importMetadata(). Take a look at MetadataStore Class API for more info.

Entity Framework creating new record instead of modifying existing one

I'm using Entity Framework with an AS.NET MVC application. I need to allow the user to create new records and modify existing ones. I am able to fetch existing records no problem, but when I pass back in the edited entity and try to save it it creates a new one and saves it and leaves the original unmodified.
I am getting the object from EF using the primary key (e.g. ID number for an employee record). I successfully retrieve it, and set the MergeOption like so:
Context.Sector.MergeOption = MergeOption.NoTracking;
I am able to trace that the object has the correct data (using the key of the original record) all the way down to the point where I call:
Context.SaveChanges();
However, after that, the new record is created instead of modifying the existing one.
Is there something obvious I am missing here? I would have thought that retrieving the object and changing some of its values (not the ID) and saving it would just work, but obviously not.
Thanks,
Chris
"NoTracking means that the ObjectStateManager is bypassed and therefore every access to the Entity Objects results in a fetch from the database and the creation of new objects."
-- http://blog.dynatrace.com/2009/03/11/adonet-entity-framework-unexpected-behaviour-with-mergeoptions/
I don't think NoTracking is what you want.
From your comment: "distributed across various tiers and some proprietary libraries"
Are you new()ing up a ObjectContext, closing it or losing the reference to it, and then trying to save your object to a new() or different ObjectContext?
If so your losing all of your change tracking information. If this is the case then you want to call the Attach() method to reattach the entity to the context, ApplyPropertyChanges() and then finally SaveChanges().
Julie Lerman has a pretty good blog post that outlines all the different change tracking options and techniques that are available. You should also check out this MSDN article on the same subject.

Resources