Reputation: 9472
I'm learning Core Data with Swift and I want to see what is currently saved in my ManagedObjectContext. The viewDidLoad() includes println(managedObjectContext!)
which outputs something like <NSManagedObjectContext: 0x1701f8500>
to the console.
How do I see what the actual attributes of my LogItem are?
Thanks
Upvotes: 1
Views: 323
Reputation: 2417
_NSManagedObjectContext_
manages the _NSManagedObjects_
by actually loading those objects into the memory under the same context. So if you want to display the content what your object model holds then you can access the _registeredObjects_
property of managedObjectContext. But remember that it will enlist only/all those objects which has been loaded to context.
println(managedObjectContext!.registeredObjects);
Upvotes: 3
Reputation: 1860
You can see a managed object context as a workbench on which you work with your model objects. You load them, you manipulate them, and save them on that workbench. Loading and saving are mediated by the persistent store coordinator.
let fetchRequest = NSFetchRequest(entityName: "Entity")
if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [Entity] {
for info in fetchResults {
println(info.valueForKey("attribute"));
}
}
Upvotes: 2
Reputation: 7944
Managed objects are not being saved in NSManagedObjectContext
. They are saved in a persistent store (like SQLite database). NSManagedObjectContext
is used for loading managed objects from the persistent store into memory and making changes to them. Then you either save the changes back to the persistent store or discard them.
If you want to load managed objects (instances of an entity called YourEntity
) from the persistent store into NSManagedObjectContext, use NSFetchRequest
:
let fetchRequest = NSFetchRequest(entityName: "YourEntity")
if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) {
println(fetchResults);
}
If you want to see the objects that are currently loaded into NSManagedObjectContext, you can use registeredObjects
method. If you want to see only updated / inserted / deleted objects, there are methods for that, called: updatedObjects
, insertedObjects
, deletedObjects
.
Upvotes: 5