How do I unload a record from the Ember Data store if I don't have the record - ember-data

I am using Ember Data revision 8 and calling the "find" method on the store with an ID.
If the server does not find a record for this ID then I'm not sure how to let my controllers and views know.
The find method in the adapter does not receive an instance of the record so if the server returns an empty hash I can not access the record's state manager to transition it into another state.
I was looking at the Store and did not see any method I could call directly from my adapter to say "Hey I did not find a record with this ID for this Type. Move it [record in the record cache] out of loading into deleted". I guess I could call Store.load(...) to transition the record but this sounds like a dirty workaround.
At the moment, the record's "isLoaded" computed property will always be false which means that if I implement some kind of spinner based on this property then it will just hang.
Has anyone faced this before?
Thanks

Related

Multiple Dbset/entity modification with single call to SaveChanges()

I am working on a .NET Core Web API which needs to interact using EF Core 5.0.2 with an Azure SQL database.
I have different repository methods where I am interacting with DbContext to add/edit/delete records for different DbSet.
For example:
UserRepository.AddUser(userdata);
Implementation of AddUser is like this,
ourDbContext.UserTable.AddAsync(userdata);
So in user service method, am calling different repository method sequentially and none of those methods call ourDbContext.SaveChangesAsync() individually. A single call to SaveChanges is present after all the repository methods calls which is acting like a unit of work pattern for all the calls as single transaction.
Example:
UserRepository.AddUser(userdata);
ActivityRepository.AddActivity("New User got added");
ourDbContext.SaveChangesAsync();
So my question is: if any saving changes to any of the tables/entities fails, will the previous successful tables change will be rolled back?
For example, suppose this operation
UserRepository.AddUser(userdata);
was successful and the new user record was added to the User table.
But this was not successful:
ActivityRepository.AddActivity("New User got added");
So no activity record was added to the Activity table.
Will SaveChangesAsync() be able to handle this situation automatically and will roll back User table new changes as well?
If not are we supposed to wrap the above codes with transaction scope? Or what is the recommended way to do it.
Briefly how DbContext's Change Tracker works:
You load entities: ChangeTracker remembers current values of all loaded entities (except you use AsNoTracking())
You have modified loaded entities, delete, add new.
You call SaveChanges: ChangeTracker starts searching which objects are changed since last load by comparing with previous values.
DML SQL is generated and everything saved in one SQL statement or in several statements in Transaction.
So, if you have one DbContext for each repository, you do not need to worry about rollbacking, just do not call SaveChanges(). For sure for restart process, you have to recreate DbContext because it contains not needed state.

IBM Worklight - JSONStore logic to refresh data from the server and be able to work offline

currently the JSONStore API provides a load() method that says in the documentation:
This function always stores whatever it gets back from the adapter. If
the data exists, it is duplicated in the collection". This means that
if you want to avoid duplicates by calling load() on an already
populated collection, you need to empty or drop the collection before.
But if you want to be able to keep the elements you already have in
the collection in case there is no more connectivity and your
application goes for offline mode, you also need to keep track of
these existing elements.
Since the API doesn't provide a "overwrite" option that would replace the existing elements in case the call to the adapter succeeds, I'm wondering what kind of logic should be put in place in order to manage both offline availability of data and capability to refresh at any time? It is not that obvious to manage all the failure cases by nesting the JS code due to the promises...
Thanks for your advices!
One approach to achieve this:
Use enhance to create your own load method (i.e. loadAndOverwrite). You should have access to the all the variables kept inside an JSONStore instance (collection name, adapter name, adapter load procedure name, etc. -- you will probably use those variables in the invokeProcedure step below).
Call push to make sure there are no local changes.
Call invokeProcedure to get data, all the variables you need should be provided in the context of enhance.
Find if the document already exists and then remove it. Use {push: false} so JSONStore won't track that change.
Use add to add the new/updated document. Use {push: false} so JSONStore won't track that change.
Alternatively, if the document exists you can use replace to update it.
Alternatively, you can use removeCollection and call load again to refresh the data.
There's an example that shows how to use all those API calls here.
Regarding promises, read this from InfoCenter and this from HTML5Rocks. Google can provide more information.

Sencha Touch 2 - "quietly" delete and update records in localstorage

I have a sencha touch 2 web app that is using a localstorage datasource to store a bunch of records.
I am able to perform all the usual crud operations fine, but I want to sync data using a webservice. so periodically, the sencha app will poll the webservice for data changes and then make the necessary changes to the localstorage datasource of my sencha app..
My approach has been to use the following code block to run my sync process every 60 seconds:
var timerID = setInterval(function()
{
MyApp.app.BackgroundProcessingMain();
}, (60000));
Inside "BackgroundProcessingMain()", I have various method calls to sync the various datastores (5)..
I call the webservice and get the data I require back, and then my approach has been to loop through the returned data, filter my store to the id of the current item of the returned data and then either delete it, or update it as necessary.
This works fine.. BUT, if this background process kicks off and I'm viewing a bound list control, my list which is using a filtered version of my datasource, suddenly drops down to only showing a single item, usually the last one in the returned data that needs to be synchronised since it was the last one that my update process filtered the store to operate on.
I thought I could use store.findById, get the record reference and update/delete that way, but if the particular ID is already being filtered out due to the view my bound list requires, the record isn't found in the store and therefore doesn't get updated..
What I'd like to be able to do is get a temporary copy of the store, unfiltered, be able to modify it, and then when my app then queries the localstorage next time a form is shown, it will just get the new updated data..
That is basically what I'm referring to as "quietly" in the title..
Does anyone have a suggestion as to what process I could take to get this update done..??
If you have example code, that would be awesome, but pseudo-code is fine..
Thanks
You can use suspendEvents() and resumeEvents() to temporarily prevent your store from firing events. You can then clear your filters, apply your updates (using store.findById()), and reapply your filters without your list changing.
var store = Ext.getStore('myStore');
store.suspendEvents();
store.clearFilter();
doThings(...);
store.filter(myFilters);
store.resumeEvents();
If you pass true into store.resumeEvents(), the buffered events will be discarded.

Core Data NSManagedObject - tracking if attribute was changed

I have an object - Config. I want to know if the Account attribute on Config has changed. When this happens, I want to send a NSNotification so that all code that cares about when the Account changes will know. My initial thought was in my NSManagedObject subclass that I would override the setAccount method to set a transient attribute AccountDidChange to true. Then in didSave if AccountDidChange was true I would send the notification and then set it back to false. Is there a better way? My issue though is that from what I've read, by changing AccountDidChange back to false, I would have dirtied my object and need to save again.
A little more info:
The Config object is the current configuration of the application. Account could actually be changed to ActiveAccount. There is a relationship to the Account Entity that has a list of all Accounts. The idea is that the user can change the active account of the application. So we have a set of servers and the user can only be logged into one at a time. Config.Account points to that active account and it is used to setup connections to the server to retrieve information. I am using this notification that Config.Account has changed to tell other objects to clean up their information - like list of alerts. Basically, all information is per Account so it needs to be removed and then refetched on its next load with the new active account.
Also, the given names are not my actual object names - just trying to make the example easier to follow.
Take a look at KVO (Key-Value Observing): Key-Value Observing Programming Guide. That's the standard way to do this in Cocoa, and is a fundamental technology that you need to understand to be a good Cocoa programmer.
KVO will let objects that care about changes to the Account property (which you should probably name account, not Account) register to be notified when the property is changed. KVO will "just work" for standard NSManagedObjects, without any additional work on your part.
The relevant methods are as follows:
-addObserver:forKeyPath:options:context: which you call on your Config object to set up the observation
-observeValueForKeyPath:ofObject:change:context: which will be called on the observer object anytime an observed value is changed
-removeObserver:forKeyPath: which you need to make sure you call when the observer no longer needs change notifications (including before the observer is deallocated).
This is all described in a lot more detail in the linked documentation.
EDIT BELOW:
Without knowing anything about your application, it's hard to know why you'd want to be notified only upon save. NSManagedObjectContext posts NSManagedObjectContextWillSaveNotification and NSManagedObjectContextDidSaveNotification. The notification's userInfo has arrays containing inserted, updated and deleted objects, but the notifications aren't as fine-grained as individual properties. I suppose you could manually keep track of changed accounts between didSave notifications. That'll probably get inefficient if you have lots of Configs in your store.
Changes to NSManagedObjects are immediate, they're just not saved to the persistent store until you call save: on the managed object context. Perhaps if you explain more about exactly what you're trying to accomplish and why, I can better help.

Refreshing DataGridView After Editing a Record

I have a DataGridView bound to a datatable which comes from an SQL Server database.
When the user edits a record my update statement changes the field datetimemodified to reflect the last date and time the record was edited (as stored procedure). The new value for datetimemodified is not brought into my DataGridView.
1) How do I refresh a DataGridView bound to a DataTable? Is there any way to refresh or resync only records that have changed instead of the entire DataTable? (Note: my update statement is working fine. I'm only wondering about refreshing the DataGridView.)
2) Would it be better to change the value of DateTimeModified on the client side so that I can avoid a refresh (assuming that this is the only reason I need to refresh the data)?
fnDraw
function will help you
oTable.fnDraw();
if you using ajax to edit. you can call this function in ajax success
The scenario you described requires to use Domain Objects that implement INotifyPropertyChanged interface. This will allow any change in Domain object to be propagated back in UI. use Domain object with BindingList. Datatables provide flexible but inefficient approach of data-binding. You may find following resource helpful.
How to Implement INotifyPropertyChanged Interface
Datatable vs BindingList