I am working on a document based application.
When opening up a saved file, I load in the NSManagedObjectContext to a view controller. This view controller needs to observe changed on the property "depth" on all objects of a certain type. When the view controller gets the context, it gets all these objects, adds itself as an observer of that value on each, and keeps them in an array to keep track.
Whenever Core Data sends the ContextDidChange notification, I add created objects to this array after observing them. For the deleted objects, I remove the view controller as an observer and remove them from that array.
This works great until I close a document and reopen it. When this happens, all the objects are added to this array. The observation works fine. However, the second the first "NSManagedObjectContextObjectsDidChangeNotification" comes in, all of my NSManagedObjects are somehow no longer in the array I set up. On delete, I get a crash telling me I can't remove an observer that's not an observer.
It's very strange.
Why are those NSManagedObjets suddenly gone? I don't release the array or do any funny business with it at all.
When you close a document and reopen it, you are getting a new view controller object.
However, it sounds like the original view controller is still around and still observing:
NSManagedObjectContextObjectsDidChangeNotification.
Check to make sure that when you close your document that the view controller is removing itself as a notification observer.
Related
After closing a CollectionView in my project, the collection it makes reference to is still available. Opening again the CollectionView duplicates the collection as it reloads the data again. I think this is expected behaviour as in the documentation it says that all the CollectionView does on close is:
unbind all listenTo events
unbind all custom view events
unbind all DOM events
unbind all item views that were rendered
remove this.el from the DOM
call an onClose event on the view, if one is provided
I'm guessing it's on me to manage the collection on the onClose event handler. Is there a good way of deleting the collection and models associated with the view?
In your situation it's not necessary to delete the collection. Variables (in this case your collection) that are not directly referenced by another object will be garbage collected by javascript. So when the view and module close and nothing else is referencing the collection it will be removed.
If you want to make absolutely sure you won't get any zombie events you can clear it's event listeners when you close the module and/or view:
myCollection.off();
Of course is your collection is a global variable (technically a property of the window object) then yes it might be a good idea to delete it like so:
delete window.myCollection;
Aside from this, removing it from memory is not something you need to worry about as the other objects referencing are closed...
I'm creating a Django/JQuery/MySQL application where I pass a composite data structure 'grid' as
return render_to_response('products.html', grid)
I render 'grid' into a set of UI elements ('td', 'button', 'div' etc. encapsulated in a HTML 'table'.
A typical use case:
User clicks on a certain UI element in the table
jQUery.click() is called which creates a inner 'input' tag for the clicked element.
User can add/modify/delete text from the element.
When focus is lost, jQuery.blur() is called which reverts the original properties of the clicked element such as removing input tag etc.
jQuery.blur() also calls a AJAX function where I do a .post call to send in user modified data back to a URL (function in view).
The called function in view then commits the new changes in database and returns a 'success' event back to web page:
tc_model_instance.update(tc_id=json_data['id'])
Through this use case, as you can see the changes are immediately committed to the database as soon as user eneters data and gives up focus on a particular element. Without using DB transactions in INNODB, how do I go about creating a View-Template association such that any changes in HTML template are asynchronously reflected in the model, but not necessarily written into the database.
A related question:
If possible I'd also like to create a event based bi-directional association between rendered template and my data structures as part of the view in such a way that any changes made either in web browser's UI element or associated view's data are always in sync. I plan to use AJAX for the most purpose. Not sure if forms would make sense in this regard.
Thanks.
You could probably throw a copy of the object into the session map and all posts alter that object instead of the DB directly. Then when the user presses a save button you'd fire off another POST command to the server that would then just call session['my_object'].save().
Note though that the session object is also saved in the DB, so if you are trying to avoid hitting the DB totally what I wrote above wouldn't help.
I'm working on a application with the this interface (sorry the language is Dutch):
http://www.flickr.com/photos/pluueer/5756159100/
The Add function (incl. the four NSTextFields) under the NSTableView are to moved to a sheet someday, but for now this is fine. I've set up bindings according to a tutorial (http://cocoadevcentral.com/articles/000080.php), but the tutorial doesn't supply how to add rows in the way I want to (just adds an empty row which you need to edit in the NSTableView).
I've got a connection between the 'Voeg toe' (Dutch for 'Add') button and the Array Controller. But after clicking I get the message:
2011-05-28 23:37:56.149 Hop Calc[4345:a0f] -[__NSPlaceholderDictionary initWithObjects:forKeys:]: number of objects (0) not equal to number of keys (4)
It makes sense, because I've not implemented anything for adding rows, but I just don't know how.
"Add a row to the table" is the wrong way to think of it. Your table represents a collection and a controller provides the information to the table, mediating between the table (view) and the collection (model). Since you mentioned bindings, the collection is likely managed by an NSArrayController. So you want to add a new object (of the kind your array controller manages) to the array controller's content array.
Simplest way: Connect the Add button to the -add: action of the NSArrayController. It'll add an empty row.
If you want more control, connect the Add button to your own custom action in some controller. That action will create an instance of whatever's represented by your array controller, prepopulate it (or whatever you want to do), then, using an outlet it holds to your NSArrayController, will call the array controller's -addObject: method to add the object (the possibly a -rearrangeObjects call to get the array controller to re-sort its contents).
Suppose I several tags, text input, radios, etc referencing one instance object. I would like to use a dropdown on that page to switch the references mentioned before to another instance of the object.
This part is pretty easy with change listener. However, the problem I have is the values currently displayed on the screen are getting set into the new, second instance.
I would like the current values to be stored in the current instance, and then the when the page refreshes I would like the new values to come from the new instance and be displayed on the screen.
Is there was way to do this? Does my question make any sense?
Grae
A value change listener is exactly the wrong tool for this, because it gets called before the values are set.
What you want is an action listener, which will get call after the values from the page are validated and set.
I'm setting up an edit window for a player to edit his user data. I've got all of the fields on the edit form bound to the appropriate Core Data entity (via an NSArrayController), and I've got an awakeFromNib method installed to handle calling the record, but I'm not sure what to put inside the method to get the record to display.
Ultimately, my goal with this is to set it up so that the application checks whether an entry exists for the user, and create one for him if there's no entry in the table.
You should look at using a NSObjectController or NSArrayController rather than binding directly to the NSManagedObject. The controllers work properly with bindings and your data will display nicely and changes you make will be propagated via the controller to your NSManagedObject.
You can set the object used by the controller in your awakeFromNib. On NSArrayController use the setSelectionIndex:(NSUInteger) index message and then to avoid empty selection send it the setAvoidEmptySelection:TRUE message
Hope that helps.