【问题标题】:Fetch Data from multiple Entities with no relationship between them从多个实体中获取数据,它们之间没有关系
【发布时间】:2014-06-21 14:18:36
【问题描述】:

我必须从与 iPhone 应用程序的不同屏幕关联的多个实体中获取数据,现在场景如下,当用户按下同步按钮时,我必须从所有这些实体中获取数据(我有大约 12实体)并通过 Web 服务将所有这些数据发送到服务器,并且所有这些实体之间没有任何关系,现在我的问题是执行此任务的最佳方法是什么,我是否应该在一个方法中编写 12 个不同的获取请求,或者是否有其他更好的方法,如果有人可以通过一些教程链接进行指导,我将非常感激,谢谢。

【问题讨论】:

    标签: ios objective-c core-data ios7


    【解决方案1】:

    您可以像这样使用 for 循环来完成您的任务。我已使用此代码删除数据库的所有条目。

    NSArray *allEntities = [[[[UIApplication sharedApplication] delegate] managedObjectModel] entities];
    NSError *error;
    for (NSEntityDescription *entityDescription in allEntities)
    {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setEntity:entityDescription];
    
        fetchRequest.includesPropertyValues = NO;
        fetchRequest.includesSubentities = NO;
    
        NSArray *items = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
        if (error) {
            NSLog(@"Error requesting items from Core Data: %@", [error localizedDescription]);
        }
    
        //Do whatever you need to do here
    }
    

    【讨论】:

    • 这个 items 数组将包含什么?所有实体??这两条线在做什么? fetchRequest.includesPropertyValues = 否; fetchRequest.includesSubentities = NO;
    • 不,items 将包含单个实体的所有对象。但是由于您正在运行 for 循环,因此在下一次迭代中它将包含另一个实体的所有对象。
    • 好的,但是我怎么知道当前项目包含哪个特定实体,实体 A、B 或 C,哪个实体?,以便我可以获取该特定实体的数据。
    • [实体描述名称];会给你实体的名称。
    猜你喜欢
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多