Re: Randomly shuffle my core data dataset?
Re: Randomly shuffle my core data dataset?
- Subject: Re: Randomly shuffle my core data dataset?
- From: Joshua Scott Emmons <email@hidden>
- Date: Wed, 29 Mar 2006 15:43:50 -0600
Be aware! I'm on the road and typing this directly into email. Be
prepared for typos!
I'm not sure if it's good design sense, but I would be tempted to
shuffle my NSManagedObject instances rather that my array controller.
Could you point to me where to do that? How can I get that collection
of NSManagedObject(s). Which method, in which instance is the best
place to do this?
Basically, you just want an array of all your Card instances, right?
You could always fetch them. Something like the following:
NSManagedObjectContext *moc = [self managedObjectContext]; //Assumes
we're in NSDocument subclass
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSArray *fetchResults;
@try {
[fetchRequest setEntity:[NSEntityDescription
entityForName:@"Card" inManagedObjectContext:moc]];
fetchResults = [moc executeFetchRequest:fetchRequest error:nil];
} @finally {
[fetchRequest release];
}
If you already have an outlet to an NSArrayController that is
handling your card instances, you could get the array by doing
something like:
NSArray *myArray = [myArrayControllerOutlet arrangedObjects];
if you have some base entity ("deck"?) that has a to-many
relationship ("cards") to you Card entities, you could KVC that
entity to get a set:
NSSet *mySet = [deckManagedObject valueForKey:@"cards"];
Once you have your array or set or whatever, just enumerate over it.
NSEnumerator *enumerator = [fetchResults objectEnumerator];
NSManagedObject *manObj;
while(manObj = [enumerator nextObject]){
[manObj setValue:someShuffledValue forKey:@"index"];
}
I would think that the best place to do this would be in your
NSPersistantDocument subclass if you're in a document-based
application. Create an action called -shuffle: and hook a button up
to it. Whenever the button is pressed, the cards get their index
properties shuffled (which will cause their order to be shuffled, if
you're sorting by index).
Again, I would do this in the model. Assuming that your card entity
has four properties like, answer1, answer2, etc., just shuffle those
properties.
OK, great approach, how can I update the model? Do I do that in the
app delegate? In which method should I put the 'shuffle' technology
in?
"How?" Get a reference to your NSManagedObject instance and use KVC
on it. If you're using an NSArrayController to order and sort your
Card instances, and you want to shuffle the selected card, do
something like:
[myArrayControllerOutlet setValue:myShuffledValue1
forKeyPath:@"selection.answer1"];
[myArrayControllerOutlet setValue:myShuffledValue2
forKeyPath:@"selection.answer2"];
etc.
"Where?" depends largely on when and how you need the answers
shuffled. I really don't know enough about your app to answer this
for you. My rule of thumb is to put code that modifies my model in my
NSDocument subclass because:
o NSDocument is a controller, and code like this that acts as "glue"
between the view and the model belongs in a controller.
o NSDocument is, specifically, a model-controller. And seeing as our
"glue" directly effects our model, NSDocument seems like a good place
for it.
o NSDocument (NSPersistentDocument, actually) has a reference to the
Managed Object Context, which you need to fetch, create, or delete
things from your model.
Hope that helps a little bit. If you get confused, it sounds as
though it wouldn't hurt if you were to take a refresher course in
bindings, NSController subclasses, and KVC/KVO. Those are the
technologies that do most of the heavy lifting when it comes to
actually moving things in and out of your Core Data model.
Cheers,
-Joshua Emmons
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden