如何将 CoreData 获取结果放入(多维)数组中?
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我强烈建议不要追求这种设计模式。通过创建多个或多维数组,您的内存会被存储在核心数据持久存储中的数据弄乱。内存问题很可能。
最好使用某种
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 fromUITableView
s, and retrieve the data for each date in your calendar as you need it. With afetchedResultsController
this is quite easy to achieve withNSIndexPath
s or some other scheme suitable for your calendar.