简体   繁体   中英

How to fetch results from CoreData in a segue

I have an iOS app using CoreData and Restkit 0.20. The main view controller is a collection view that is populated by a request to my server using Restkit. The request to the server returns an image and title to be placed in each cell. If a user selects a cell I need to make a second request to the server for the details about the chosen cell.

I have spent the evening attempting to accomplish this in the prepareForSegue method. The problem is I can't seem to figure out how to get the results from the request that takes place in prepareForSegue .

Here is the prepareForSegue method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"newsDetail"]) {

// I USE THIS PORTION TO GET DATA THAT IS NEEDED FOR THE GET REQUEST TO THE SERVER AND TO PASS SOME ADDITIONAL ITEMS FROM THE FIRST REQUEST TO THE DETAIL VIEW CONTROLLER
        NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] lastObject];
        NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        NSString *articleID =[object valueForKey:@"gistID"];
        NSString *personID = [object valueForKey:@"userID"];

// I USE THIS LINE TO ASSEMBLE THE STRING FOR getObjectsAtPath BELOW
        NSString *getArticle =[NSString stringWithFormat:@"/rest/article/getArticleForView?aid=%@&pid=%@&ip=255.255.255.0",articleID,personID];

//HERE I MAKE THE CALL TO THE SERVER USING RESTKIT METHODS        
        [[RKObjectManager sharedManager] getObjectsAtPath:getArticle parameters:nil success:nil failure:nil];    

        [[segue destinationViewController] setDetailItem:object];
    }
}

Here is how I load it in the detail view controller.

- (void)configureView
{
    if (self.detailItem) {
        self.newsDetailText.text = [[self.detailItem valueForKey:@"fullArticle"]description];
        NSURL *photoURL =[NSURL URLWithString:[self.detailItem valueForKey:@"imageUrl"]];
        NSData *photoData = [NSData dataWithContentsOfURL:photoURL];
        self.newsDetailImageView.image =[UIImage imageWithData:photoData];
    }
}

It will segue properly and the image (which was already persisted from the first request) will be loaded but the text (which was retrieved from the request at segue) will not. In fact the valueForKey fullArticle is null.

It's probably pretty obvious I'm a rookie based on the question. With that in mind code snippets are VERY helpful. Thanks!

Since you are doing an asynchronous request, the data you wanna show are being arrived after the destinationViewController actually appeared. So, you should pass a success block to your request and update your view inside it. Something like this:

UIViewController *viewController = [segue destinationViewController];
[[RKObjectManager sharedManager] getObjectsAtPath:getArticle parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    if (!operation.error) {
        viewController.newsDetailText.text = mappingResult.firstObject[@"fullArticle"];
    }
} failure:nil];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM