Pregunta

I embedded 'Core Data' into my app. I use a predicate and fetch the result. I use a mutableArray called "fetchedObjects" to get the (predicated and then fetched) result. Now I need to pass this result to another view controller. How can I do that?

1.First thing I tried is using 'NSCoding' but it didn't work out. I think Core Data doesn't comply with NSCoding, am I right?

If I can use 'NSCoding', how do I do it? I tried to save and then load the fetchedObjects but it didn't work out.

2.Off course I can define a pointer using

"product = [[Product alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];" 

but how can I get just the "fetchedObjects" mutableArray but not all the objects?

3.Do you know a better way to pass a NSMutableArray having NSManagedObjects in it? and how can we pass the NSManagedObjects to the other view controllers, only in the context?

Thanks!

¿Fue útil?

Solución

You could use an NSNotification.

Say View Controller A fetches the results, and View Controller B needs them.

in VCA:

NSArray *data = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

[[NSNotificationCenter defaultCenter]
 postNotificationName:@"newFetchedDataNotification"
               object:nil
             userInfo:@{@"data": data}];

in VCB:

in viewWillAppear, start listening for the notification:

 [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(processData)
                                              name:@"newFetchedDataNotification"
                                            object:nil];

and add a method:

- (void)processData:(NSNotification *)notification
{
    NSArray *data = [notification.userInfo objectForKey:@"succeeded"]
}

And don't forget to unregister for notifications in viewWillDisappear:

    [[NSNotificationCenter defaultCenter] removeObserver:self];

I still think the preferred way is using an NSFetchedResultsController to do a first fetch and be notified when objects change. Take a look at this tutorial:

http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller

PS: NSManagedObjects are just like any other objects, no need to use encoding. You need to be careful when modifying and saving them though!

Otros consejos

Rather than passing the managed objects directly, if all them are already saved I would pass the ObjectID or the unique url, and have the receiver retrieve them from the store.

Retrieving is fast, and also will avoid many problem that may occur with concurrency.

If the receiving controller just need to display data, I would also think about retrieving just the properties you need with an NSDictionary result type, and pass the resulting array to the controller. But of course, I don't know anything about your design.

UPDATE

If I understood correctly your comment, the big advantage of NSFRC is the bunch of delegate methods it brings with it. If you made your UIViewController the delegate of your NSFRC which is NSFetchedResultsControlerDelegate then the fetched controller itself will invoke your view controllers implemented delegate methods, as soon as there's a change in the model. Then within this methods you have to refresh your table view. The view is not going to be refreshed by itself.

The difference is that if your UITableView datasource is just an array of managed objects, you would need to build by yourself all the logic to react to model change, recognize which cell need to be refreshed, then decide if it deleted/updated/moved etc.. if you think about it, it is quite a few logic to be implemented, and will not be as efficient as the NSFRC is which is already customized for this purpose.

Also, by reading the rest of your comment, you cannot pass a NSFetchedResultsController, well I suppose you can, but it is useless, the NSFRC is meant to be created and used in the UIViewController on screen. The NSFRC is instantiated with few parameters, one of them is a NSFetchRequest, so you have to customize your request to retrieve the objects you need.

P.S. Sorry, at the moment I am behind a company firewall which is blocking many sites (including Apple docs), therefore I cannot give you any links.

In simple way ,

VC2.h

@property (strong) NSMutableArray *device;

VC2.m

@synthesize device;

Now in Your VC1 from which you want to pass array

    VC2 *v2=[VC2 alloc]initWithNibName:@"VC2" bundle:nil];
    v2.device = array;
    [self.navigationController pushViewController:v2 animated:YES];

or if you are not using Navigationcontroller.

[self presentViewController:v2 animated:YES completion:nil];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top