This seems like such a simple thing to do but I just can't put it together. I have an iOS app using RestKit 0.20 to populate my CoreData attributes. My main view controller is a collection view which is populated at startup by making a request to the server.
When a user selects a cell I am able to transfer the data contained in that cell to the detail view (an image and a title). But I also need to make another request to the server to obtain all of the information to be shown in the detail viewController. The Entity name is Gist
and the Attribute is fullArticle
Here is the segue to the detailViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"newsDetail"]) {
NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] lastObject];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[[segue destinationViewController] setmanagedObjectContext:managedObjectContext];
[[segue destinationViewController] setDetailItem:object];
}
}
Here is what i use in -(void)viewDidLoad
{
[super viewDidLoad];
NSString *articleID =[self.detailItem valueForKey:@"articleId"];
NSString *personID = [self.detailItem valueForKey:@"userID"];
NSString *getArticle =[NSString stringWithFormat:@"/rest/article/getArticleForView?aid=%@&pid=%@&ip=255.255.255.0",articleID,personID];
[[RKObjectManager sharedManager] getObjectsAtPath:getArticle parameters:nil success:nil failure:nil];
NSURL *photoURL =[NSURL URLWithString:[self.detailItem valueForKey:@"imageUrl"]];
NSData *photoData = [NSData dataWithContentsOfURL:photoURL];
self.newsDetailImageView.image =[UIImage imageWithData:photoData];
//This is where I am stuck, How do you fetch the Attribute "fullArticle" from the request that I just made?
self.newsDetailText.text = //valueForKey:"fullArticle" from article with ID `articleID`
}
I have tried using the code below but the fetchedResultsController
is setup for use with a tableView
so there is no way I know of to specify without an indexPath
NSManagedObject *detailText = [self.fetchedResultsController objectAtIndex:0];
self.newsDetailText.text = [[detailText valueForKey:@"fullArticle"] description];
When tried this way fullArticle
is null presumably because objectAtIndex:0
is not the right way to designate what object I need to fetch.
Please shed some light on this for me! Clearly a rookie question so code snippets really help! Thanks in advance!
Solution with help from Wain
Wain pointed out that the request does not take place immediately. I needed to use the callbacks in the request to acquire the data I was wanting. Restkit provides the mappingResults
in a variety of ways. You can read about that here under RKMappingResult
.
Here is how I made it work.
//I REPLACE MY SERVER REQUEST WITH THIS
[[RKObjectManager sharedManager] getObjectsAtPath:getArticle parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
//HERE IS HOW I SET THE TEXT FIELD IN THE DETAIL VIEW CONTROLLER
self.newsDetailText.text = [[mappingResult.firstObject valueForKey:@"fullArticle"]description];
// ERROR HANDLING
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];
The request you make doesn't get the results from the server immediately. You should implement the callbacks to handle success and error responses and use the supplied mapping result to get the info to display.
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.