如何从 Core Data 中检索最新的对象?

发布于 2024-09-18 01:14:54 字数 1835 浏览 3 评论 0原文

我正在编写一个使用 UIPickerView 的应用程序,并将对选取器的每个更改存储在核心数据中(该应用程序的名称在 iTunes 商店中为 Wizard Blood)。到目前为止,我已经取得了一些成功,我的 -didSelectRow 方法通过以下代码成功地将新对象添加到核心数据:

// Get context, entity, managedobject from the fetchedResultsController

    [newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];

    //Iterates through each component and sets the value to the appropriate key
    for (int j=0; j < [LifeCounter numberOfComponents]; j++) {
        NSString *keyName = [NSString stringWithFormat:@"lifeTotal%i",j];
        [newManagedObject setValue:[NSNumber numberWithInteger:[LifeCounter selectedRowInComponent:j]] forKey:keyName];
    }

    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Error saving entity: %@", [error localizedDescription]);
    }   

但是,在我的 -viewDidLoad 中,我似乎无法获取核心数据来从最新对象加载值。这就是我到目前为止所拥有的:

lifeCounterArray = [[NSMutableArray alloc] init];

for ( int i = 200; i >= -200; i-- )
{
    NSString *myString = [NSString stringWithFormat:@"%i", i];
    [lifeCounterArray addObject:myString];
}

//Hopefully this retrieves the latest values from the core data store
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

//Iterates through each component, setting it to the stored value in core data
for (int j=0; j < [LifeCounter numberOfComponents]; j++) {
    NSString *keyName = [NSString stringWithFormat:@"lifeTotal%i",j];
    NSInteger row = [[managedObject valueForKey:keyName] integerValue] +180;
    [LifeCounter selectRow:row inComponent:j animated:NO];
}

现在,我很确定我的 fetchedResultsController 方法正在工作,因为我可以看到它创建对象(我可以在 sqlite 数据库中看到它们),但也许我什至不应该使用 fetchedResultsController 因为我实际上只关心最新的对象。如果需要的话,我很乐意发布我的 fetchedResultsController 方法,提前感谢您的帮助。

I'm writing an application that uses a UIPickerView and is storing each change to the pickers in core data (the name of the app is Wizard Blood in the iTunes store). I've had some success so far in that my -didSelectRow method successfully adds new objects to core data via the following code:

// Get context, entity, managedobject from the fetchedResultsController

    [newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];

    //Iterates through each component and sets the value to the appropriate key
    for (int j=0; j < [LifeCounter numberOfComponents]; j++) {
        NSString *keyName = [NSString stringWithFormat:@"lifeTotal%i",j];
        [newManagedObject setValue:[NSNumber numberWithInteger:[LifeCounter selectedRowInComponent:j]] forKey:keyName];
    }

    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Error saving entity: %@", [error localizedDescription]);
    }   

However, in my -viewDidLoad I can't seem to get core data to load values from the latest object. This is what I have so far:

lifeCounterArray = [[NSMutableArray alloc] init];

for ( int i = 200; i >= -200; i-- )
{
    NSString *myString = [NSString stringWithFormat:@"%i", i];
    [lifeCounterArray addObject:myString];
}

//Hopefully this retrieves the latest values from the core data store
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

//Iterates through each component, setting it to the stored value in core data
for (int j=0; j < [LifeCounter numberOfComponents]; j++) {
    NSString *keyName = [NSString stringWithFormat:@"lifeTotal%i",j];
    NSInteger row = [[managedObject valueForKey:keyName] integerValue] +180;
    [LifeCounter selectRow:row inComponent:j animated:NO];
}

Now, I'm pretty sure my fetchedResultsController method is working since I can see it creating objects (I can see them in the sqlite database) but maybe I shouldn't even be using a fetchedResultsController since I really only care about the latest object. I'd be happy to post my fetchedResultsController method if needed, thanks for the help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铜锣湾横着走 2024-09-25 01:14:54

如果自上次查询以来您已更改数据,是否必须删除 fetchedResultsController 的缓存?

[NSFetchedResultsController deleteCacheWithName:@"(cachename)"];

缓存名称在 initWithFetchRequest 中设置。

如果这不起作用,请尝试在数据库更改后将 fetchedResultsController 设置为 nil,这将迫使它重做请求。

If you've changed the data since you last queried might you have to delete the fetchedResultsController's cache?

[NSFetchedResultsController deleteCacheWithName:@"(cachename)"];

The cache name is set in initWithFetchRequest.

if that doesn't work, try setting the fetchedResultsController to nil after the db changes which will force it to redo the request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文