强制使用已使用NSUserDefaults创建的实体

时间:2015-08-19 02:24:27

标签: ios objective-c cocoa

我试图选择或按一个或两个按钮,应用程序将创建与该邻域相关的实体,从而根据选择显示以下屏幕。为了避免每次创建实体都决定使用NSUserDefaults并实现如下:

- (void)carregarEntidadeLojaComId:(NSString *)identificadorLoja keyNSUserDefault:(NSString *)key {
    if (![[NSUserDefaults standardUserDefaults] objectForKey:key]) {
        NSManagedObjectContext *contexto = [self managedObjectContext];

        NSArray *arrayLojas = [Utils carregarArrayPlist:identificadorLoja];
        NSArray *atributosComuns = @[@"titulo", @"subtitulo", @"telefone", @"endereco"];
        for (NSDictionary *dicionario in arrayLojas) {
            loja = [NSEntityDescription insertNewObjectForEntityForName:@"Loja" inManagedObjectContext:contexto];
            categoria = [NSEntityDescription insertNewObjectForEntityForName:@"Categoria" inManagedObjectContext:contexto];
            quadra = [NSEntityDescription insertNewObjectForEntityForName:@"Quadra" inManagedObjectContext:contexto];

            //Loop para atributos comuns
            for (NSString *atributo in atributosComuns) {
                [loja setValue:[dicionario objectForKey:atributo] forKey:atributo];
            }

            [categoria setValue:[dicionario objectForKey:@"categoria"] forKey:@"nome"];
            [loja setValue:categoria forKey:@"categoria"];

            [quadra setValue:[dicionario objectForKey:@"quadra"] forKey:@"nome"];
            [loja setValue:quadra forKey:@"quadra"];
        }
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

第一次转向时,应用程序识别出没有密钥并输入if语句,因此,创建实体。从第二次使用应用程序开始,它就不会进入if语句,但是他不能使用已经创建的实体,因此不能满足依赖它们的屏幕。我的问题是如何强制应用程序使用第一次用户单击应用程序时创建的实体。

1 个答案:

答案 0 :(得分:2)

是否真的需要NSUserDefaults

为什么不直接从CoreData本身查询实体,如下所示:

- (void)carregarEntidadeLojaComId:(NSString *)identificadorLoja keyNSUserDefault:(NSString *)key {
    NSManagedObjectContext *contexto = [self managedObjectContext];

    NSFetchRequest *request = [NSFetchRequest new];
    request.entity = [NSEntityDescription entityForName:@"Loja"
                                 inManagedObjectContext:contexto];

    request.predicate = [NSPredicate predicateWithFormat:@"identificador = %@", identificadorLoja];

    NSError *error = nil;
    NSArray *fetchedObjects = [contexto executeFetchRequest:request error:&error];

    NSArray *lojas = nil;
    if ((error == nil) && [fetchedObjects count] > 0)
      lojas = fetchedObjects;

    if (!lojas) {
        NSArray *arrayLojas = [Utils carregarArrayPlist:identificadorLoja];
        NSArray *atributosComuns = @[@"titulo", @"subtitulo", @"telefone", @"endereco"];
        for (NSDictionary *dicionario in arrayLojas) {
            loja = [NSEntityDescription insertNewObjectForEntityForName:@"Loja" inManagedObjectContext:contexto];

            //Loop para atributos comuns
            for (NSString *atributo in atributosComuns) {
                [loja setValue:[dicionario objectForKey:atributo] forKey:atributo];
            }

            [loja setValue:[self categoriaWithNome:[dicionario objectForKey:@"categoria"]
                                        inContexto:contexto]
                    forKey:@"categoria"];

            [loja setValue:[self quadraWithNome:[dicionario objectForKey:@"quadra"]
                                     inContexto:contexto]
                    forKey:@"quadra"];


            [loja setValue:identificadorLoja
                    forKey:@"identificador"]; 

        }
    } else {
      // do what you want with loja's, previously stored in CoreData

      for (NSManagedObject *entity in lojas)
        ...
    }

}

- (NSManagedObject *) categoriaWithNome:(NSObject *)nome inContexto:(NSManagedObjectContext *)contexto {
  NSManagedObject *categoria = [NSEntityDescription insertNewObjectForEntityForName:@"Categoria" inManagedObjectContext:contexto];
  [categoria setValue:nome forKey:@"nome"];
  return categoria;
}

- (NSManagedObject *) quadraWithNome:(NSObject *)nome inContexto:(NSManagedObjectContext *)contexto {
  NSManagedObject *quadra = [NSEntityDescription insertNewObjectForEntityForName:@"Quadra" inManagedObjectContext:contexto];
  [quadra setValue:nome forKey:@"nome"];
  return quadra;
}    

<强> UPD。

已添加predicate以请求并移除limit,因为您似乎每个loja都有多个identificadorLoja