简体   繁体   English

如何从CoreData实体获取结果

[英]How to fetch results from CoreData Entity

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. 我有一个使用RestKit 0.20填充我的CoreData属性的iOS应用。 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. 但是我还需要向服务器发出另一个请求,以获取要在详细信息viewController中显示的所有信息。 The Entity name is Gist and the Attribute is fullArticle 实体名称为Gist ,属性为fullArticle

Here is the segue to the detailViewController 这是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 这是我在-(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 我尝试使用下面的代码,但fetchedResultsController设置为与tableView一起使用,所以我知道没有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. 当以这种方式尝试fullArticle为null时,大概是因为objectAtIndex:0不是指定我需要获取的对象的正确方法。

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的帮助下的解决方案

Wain pointed out that the request does not take place immediately. Wain指出请求不会立即发生。 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. Restkit提供mappingResults以各种不同的方式。 You can read about that here under RKMappingResult . 你可以阅读有关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. 您应该实现回调以处理成功和错误响应,并使用提供的映射结果来获取要显示的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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