簡體   English   中英

如何從CoreData實體獲取結果

[英]How to fetch results from CoreData Entity

這似乎很簡單,但是我無法將其放在一起。 我有一個使用RestKit 0.20填充我的CoreData屬性的iOS應用。 我的主視圖控制器是一個集合視圖,該視圖在啟動時通過向服務器發出請求來填充。

當用戶選擇一個單元格時,我能夠將該單元格中包含的數據傳輸到詳細視圖(圖像和標題)。 但是我還需要向服務器發出另一個請求,以獲取要在詳細信息viewController中顯示的所有信息。 實體名稱為Gist ,屬性為fullArticle

這是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];       
    }
}

這是我在-(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`       

}

我嘗試使用下面的代碼,但fetchedResultsController設置為與tableView一起使用,所以我知道沒有indexPath無法指定

NSManagedObject *detailText = [self.fetchedResultsController objectAtIndex:0];
self.newsDetailText.text = [[detailText valueForKey:@"fullArticle"] description];

當以這種方式嘗試fullArticle為null時,大概是因為objectAtIndex:0不是指定我需要獲取的對象的正確方法。

請為我說明一下! 顯然,這是一個菜鳥問題,因此代碼片段確實有幫助! 提前致謝!

在Wain的幫助下的解決方案

Wain指出請求不會立即發生。 我需要在請求中使用回調來獲取我想要的數據。 Restkit提供mappingResults以各種不同的方式。 你可以閱讀有關RKMappingResult

這就是我的工作方式。

//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);
}];

您發出的請求不會立即從服務器獲得結果。 您應該實現回調以處理成功和錯誤響應,並使用提供的映射結果來獲取要顯示的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM