问题
I just read the author of MagicalRecord's blog post on Why contextForCurrentThread Doesn't work in MagicalRecord.
contextForCurrentThread
is deprecated and saveWithBlock
should be used instead because it creates a safe new NSManagedObjectContext
for the relevant thread.
I've been using contextForCurrentThread
extensively in my app so far. However, I'm having trouble figuring out how to use saveWithBlock
instead as my fetching and saving are not necessarily happening sequentially.
Currently I'm doing things like:
localContext = NSManagedObjectContext.MR_contextForCurrentThread
person = Person.MR_createInContext(localContext)
person.name = "John Smith"
The user may then browse around the app, different controllers, views etc. are displayed. Other objects may be created using a similar method to the code above.
Then at some arbitrary point in the future, when the user decides to save, I run this method:
localContext = NSManagedObjectContext.MR_contextForCurrentThread
localContext.MR_saveToPersistentStoreWithCompletion(
lambda { |success, error|
# ...
}
)
What is the recommended way to create & update objects and then save them without using contextForCurrentThread
?
回答1:
So, I use objective C as opposed to RubyMotion, but you should be able todo things like this:
MagicalRecord.saveWithBlock(
lambda { |localContext|
person = Person.MR_createInContext(localContext)
#update person stuff here
}
)
EDIT
If you want to save the context later, you just need to hold on to it:
// Somewhere in your ViewController, etc
NSManagedObjectContext *context = [NSManagedObjectContext MR_confinementContext];
// Somewhere else
Person *p = [Person MR_createInContext:context];
// And in yet another method
[context MR_saveToPersistentStoreAndWait];
The main idea here is that you just need to hold on to the context and perform your operations on it when you're ready. If you want a background save to occur, you an use the following method:
[context MR_saveToPersistentStoreCompletion:^(BOOL success, NSError *error){
//called on the main thread when save is complete
}];
回答2:
here is a tutorial for new api: http://ablfx.com/blog/article/2 here is the refence: http://cocoadocs.org/docsets/MagicalRecord/2.1/
init
[MagicalRecord setupCoreDataStackWithStoreNamed:@"MyDatabase.sqlite"];
dealloc
[MagicalRecord cleanUp];
insert
Person *alex = [Person MR_createEntity]; alex.name = @"Alex"; alex.age = @23;
select
/Retrieve all for aNSManagedObject subclass
NSArray *people = [Person MR_findAll];
//Retrieve first record
Person *aPerson = [Person MR_findFirst];
//Retrieve records conditionally & sort
NSArray *people = [Person MR_findByAttribute:@"name" withValue:@"alex" andOrderBy:@"age" ascending:YES];
update
//Updating a retrieved entity is as easy as manipulating it's properties
aPerson.age = @56;
delete
//Remove all records
[Person MR_truncateAll];
//Delete single record, after retrieving it
[alex MR_deleteEntity];
save
//For any entities to actually be saved / updated / deleted on disk call following method
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
//Again check the MagicalRecord repo for more save options
回答3:
I might be mistaken here, but I think that Magical Record isn't prepared for this use case.
Here's how I'd do what you want in pure Core Data:
- Create an independent or nested context and keep somewhere (AppDelegate, singleton object, etc.) a reference to it
- Do the insertions, reads, updates and deletions with performBlock: and performBlockAndWait:
Save the context with:
[context performBlock:^{ ZAssert([context save:&error], @"Save failed: %@\n%@", [error localizedDescription], [error userInfo]); }];
This seems to be the standard approach to these kind of problems. It's nicely described online, e.g. Common Background Practices - Core Data in the Background.
来源:https://stackoverflow.com/questions/19251246/how-do-i-use-magical-record-to-create-update-objects-and-save-them-without-usi