0

コア データ モデルにオブジェクトを追加しようとしました。しかし、それらを表示しようとすると、そこにはありません。ヘルパーとしてカテゴリを使用しています。

+ (Project *)createProjectWithDictionary:(NSDictionary *)dic inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext {

    Project *project = nil;

    // Build a fetch request to see if we can find this Flickr photo in the database.
    // The "unique" attribute in Photo is Flickr's "id" which is guaranteed by Flickr to be unique.

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Project"];
    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]];
    request.predicate = [NSPredicate predicateWithFormat:@"title = %@", [dic[kTagProjectTitle]description]];

    // Execute the fetch

    NSError *error = nil;
    NSArray *matches = [managedObjectContext executeFetchRequest:request error:&error];

    // Check what happened in the fetch

    if (!matches || ([matches count] > 1)) {  // nil means fetch failed; more than one impossible (unique!)
        // handle error
    } else if (![matches count]) { // none found, so let's create a Photo for that Flickr photo
        project = [NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:managedObjectContext];
        NSLog(@"Encontrado Primera vez");

        project.projectId = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectId] intValue]];
        project.title = [dic valueForKey:kTagProjectTitle];
        project.estimatedPrice = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectEstimatedPrice] floatValue]];
        NSMutableArray *tags = [[NSMutableArray alloc] init];
        tags = [dic objectForKey:kTagProjectsTags];

        NSMutableSet *tagSet = [[NSMutableSet alloc]init];
        for (NSDictionary * tagDic in tags){
            NSString *tagName = [tagDic objectForKey:kTagProjectTagName];
            Tag *tag = [Tag insertTagName:tagName inManagedObjectContext:managedObjectContext];
            [project addTagsObject:tag];
        }

       // [project addTags:tagSet];

addObject:(NSSet *tag) で NSMutableSet を使用してみましたが、 addTagObject も使用しましたが、NSLog に同じ出力がありました。

            for( Tag *tag in project.tags){
                NSLog(@"TAG TITLES %@", tag.name);  
            }
       2013-09-30 23:03:36.003 Prototype[28959:c07] TAG TITLES iOS
       2013-09-30 23:03:36.004 Prototype[28959:c07] TAG TITLES Windows Phone
    }

次に、それらをセルに表示しようとすると。新しい NSLog があり、これが出力です。メソッドではなく、最初の NSLog でそれらを見ることができるのはなぜcellForRowAtIndexPathですか?

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   Project *project = (Project *)[self.data objectAtIndex:indexPath.row];
   NSLog(@"TITLE %@", project.tags);
}

出力

 TITLE Relationship 'tags' on managed object (0x7c60ba0) <Project: 0x7c60ba0> (entity: Project; id: 0x7c60be0 <x-coredata:///Project/tE9F74F99-04BC-43CC-BD68-F435712B3B3C9> ; data: {

    estimatedPrice = 1999;
    projectId = 418;
    tags =     (
    );
    title = "Aplicaci\U00f3n IOS";
    "yp_desc" = nil;
}) with objects {(
)}

}
4

2 に答える 2

1

タグを追加した後にコンテキストを保存していない可能性があります。

于 2013-09-30T21:29:38.123 に答える
0

Magical Record フレームワーク ( https://github.com/magicalpanda/magicalrecord ) を使用することをお勧めします。これにより、コア データの管理が非常に簡単になります。

一方、それなしで続けたい場合は、次のように変更してみます。

 + (Project *)createProjectWithDictionary:(NSDictionary *)dic inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext {

    Project *project = nil;

    // Build a fetch request to see if we can find this Flickr photo in the database.
    // The "unique" attribute in Photo is Flickr's "id" which is guaranteed by Flickr to be unique.

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Project"];
    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]];
    request.predicate = [NSPredicate predicateWithFormat:@"title = %@", [dic[kTagProjectTitle]description]];

    // Execute the fetch

    NSError *error = nil;
    NSArray *matches = [managedObjectContext executeFetchRequest:request error:&error];

    // Check what happened in the fetch

    if (!matches || ([matches count] > 1)) {  // nil means fetch failed; more than one impossible (unique!)
        // handle error
    } else if (![matches count]) { // none found, so let's create a Photo for that Flickr photo
        project = [matches lastObject];
        NSLog(@"Encontrado Primera vez");

        //Makes that only if you want to rehydrate your saved object. At other way I am not sure what yo want to do here :S
        project.projectId = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectId] intValue]];
        project.title = [dic valueForKey:kTagProjectTitle];
        project.estimatedPrice = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectEstimatedPrice] floatValue]];
        NSMutableArray *tags = [[NSMutableArray alloc] init];
        tags = [dic objectForKey:kTagProjectsTags];

        for (NSDictionary * tagDic in tags){
            NSString *tagName = [tagDic objectForKey:kTagProjectTagName];
            Tag *tag = [NSEntityDescription insertNewObjectForEntityForName:@"Tag" inManagedObjectContext:managedObjectContext];
            //TODO: populate tag object using tagDic
            [project addTagsObject:tag];
        }

現在のコンテキストを保存していない場合は、同じコンテキストで取得することを忘れないでください;)

于 2013-09-30T21:51:08.303 に答える