如何将 CoreData 获取结果放入(多维)数组中?

发布于 2024-12-22 19:14:37 字数 1994 浏览 2 评论 0原文

我想将 coredata 中的多行数据获取到多维数组中,以便我可以循环遍历它们以在日历中创建事件。然而,从对象的角度来看,拥有一个真正的多维数组似乎不可能或不明智,因此我为要用于事件属性(标题、注释、时间)的每一列数据创建了一个 NSMutableArray。

但是如何将每一列的所有值分配到它自己的 NSMutableArray 中呢?或者我应该使用 NSDictionary 来保存值?

这是我从 CoreData 获取的数据,这是非常标准的:

    MyAppDelegate       *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSFetchRequest         *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription    *entityDescription = [NSEntityDescription entityForName:@"My_List" 
                                                        inManagedObjectContext:context];
[fetchRequest setEntity:entityDescription];

NSSortDescriptor       *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"my_list_name" ascending:YES];
NSArray                *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest           setSortDescriptors:sortDescriptors];
[fetchRequest           setFetchBatchSize:20];

[sortDescriptors        release];
[sortDescriptor         release];

NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:nil
cacheName:@"my_list.cache"];

fetchedResultsController.delegate = self;
NSError *error;
BOOL success = [fetchedResultsController performFetch:&error];
if (!success) {
    //Handle the error
}

self.resultsController = fetchedResultsController;

NSArray *fetchedObjects = [fetchedResultsController fetchedObjects];

在这里,我将推测我应该为每个数组循环遍历我的 NSManagedObject,但我不确定如何进行。

  for (NSManagedObject *list in context) 
{
    [ reminderTitleMutableArray addObject:my_list_List.my_list_name ];
            [ reminderTitleMutableArray addObject:my_list_List.my_list_description ];
    [ reminderTitleMutableArray addObject:my_list_List.my_list_tminus ];
}

这是正确的方法吗?

谢谢

I want to get data from multiple rows in coredata into a multidimensional array so I can loop through them to create events in a calendar. However, it doesn't seem possible or advisable from an objects standpoint to have a true multidimensional array, so I've created one NSMutableArray per column of data I want to use for the event attributes (title, note, time of day).

But how do I assign all the values for each of the columns into its own NSMutableArray? Or should I use a NSDictionary to hold the values?

Here's my fetch from CoreData which is pretty standard:

    MyAppDelegate       *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSFetchRequest         *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription    *entityDescription = [NSEntityDescription entityForName:@"My_List" 
                                                        inManagedObjectContext:context];
[fetchRequest setEntity:entityDescription];

NSSortDescriptor       *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"my_list_name" ascending:YES];
NSArray                *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest           setSortDescriptors:sortDescriptors];
[fetchRequest           setFetchBatchSize:20];

[sortDescriptors        release];
[sortDescriptor         release];

NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:nil
cacheName:@"my_list.cache"];

fetchedResultsController.delegate = self;
NSError *error;
BOOL success = [fetchedResultsController performFetch:&error];
if (!success) {
    //Handle the error
}

self.resultsController = fetchedResultsController;

NSArray *fetchedObjects = [fetchedResultsController fetchedObjects];

Here I'm going speculate that I should loop through my NSManagedObject for each of the arrays but I'm not sure how.

  for (NSManagedObject *list in context) 
{
    [ reminderTitleMutableArray addObject:my_list_List.my_list_name ];
            [ reminderTitleMutableArray addObject:my_list_List.my_list_description ];
    [ reminderTitleMutableArray addObject:my_list_List.my_list_tminus ];
}

Is this the right way?

Thanks

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

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

发布评论

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

评论(1

多像笑话 2024-12-29 19:14:37

我强烈建议不要追求这种设计模式。通过创建多个或多维数组,您的内存会被存储在核心数据持久存储中的数据弄乱。内存问题很可能。

最好使用某种 datasource 方案(我相信您从 UITableView 中了解该方案),并根据需要检索日历中每个日期的数据它。使用fetchedResultsController,使用NSIndexPath或其他适合您日历的方案可以很容易地实现这一点。

I strongly advise to not pursue this design pattern. By creating multiple or multidimensional arrays, you are cluttering your memory with data that is anyway stored in the core data persistent store. Memory problems are probable.

It would be much better to use some kind of datasource scheme, which I am sure you know from UITableViews, and retrieve the data for each date in your calendar as you need it. With a fetchedResultsController this is quite easy to achieve with NSIndexPaths or some other scheme suitable for your calendar.

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