简体   繁体   中英

Core Data: Adding objects from NSMutableArray to NSMutableSet?

Judging from the amount of related questions that were brought up, this looks like a frequently asked question which makes me all the more hesitant to ask it. I have looked at a majority of these questions but none of them seem to address the specific problem I am having trouble figuring out.

A little background information on what I'm trying to achieve, how I am going about it, and what issue I am having:

I am trying to parse an XML web service, load that data into an NSMutableArray(Or any other place from which I can access it later), and then take my data and load it into my Core Data model. The first part of this I can do, its once I have my information in the array and trying to load it into Core Data that I cannot seem to progress.

My model(simplified for this question) consists of a route entity that has a one to many relationship with a checkpoint entity. The data I would be trying to load is a variety of attribute information into my route entity, which is not included in my array, and then my list of checkpoints, which is what the array is. My problem is that I cannot reliably add my entire array of checkpoints and then save. For the static case I am using for development, I have a consistent 20 checkpoints being parsed into my NSMutableArray, of these, the most I have been able to transfer into my NSMutableSet aka the checkpoints part of my route entity is 7 before crashing with either a SIGABRT, or EXC_BAD ACCESS, or incorrect selector sent. I have been trying to figure it out for the better part of today with no luck. Now for some code:

NSManagedObjectContext *context = [appDelegate managedObjectContext];

    //newRoute is the route that I am trying to create and then store persistently
    newRoute = [NSEntityDescription insertNewObjectForEntityForName:@"Route" inManagedObjectContext:context];

    //filling in some available attribute information
    if (name.text == @"")
        [newRoute setValue:[NSDate date] forKey:@"name"];
    else
        [newRoute setValue:name.text forKey:@"name"];

    NSMutableSet *muteSet = [newRoute mutableSetValueForKey:@"myCheckpoints"];

    for (int i = 0; i < [appDelegate.checkpoints count]; i++ )
    {
        Checkpoint *newCheckpoint = [[Checkpoint alloc] init];
        [newCheckpoint setName:[[appDelegate.checkpoints objectAtIndex:i] valueForKey:@"name"]];

        NSLog(@"Appending %@ to set", newCheckpoint.name);
        [muteSet addObject:newCheckpoint];

        [newCheckpoint release];
    }

    //myCheckpoints is the route<-->>checkpoints relationship 
    [newRoute setValue:muteSet forKey:@"myCheckpoints"];

    // Save the context.
    NSError *error = nil;
    if (![context save:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

Out of curiosity, why doesn't the following work?

NSSet *ckPtSet = [[NSSet alloc] initWithArray:appDelegate.checkpoints];     
[newRoute setValue:ckPtSet forKey:@"myCheckpoints"];

As far as I understand, and this might be where the problem is... when setting the value of myCheckpoints, the expectation is to be passed an NSSet. When going through with the debugger the initialized set actually contains the 20 objects, but when I try to step past I get the incorrect selector received error again.

Anyways, thank you for taking the time to read my wall of text, if you need more to help please let me know and I will add it asap! -Karoly

The documentation for [id<NSKeyValueCoding> mutableSetValueForKey:] states that it returns a mutable set proxy that provides read-write access to the unordered to-many relationship specified by a given key. This means that the object returned isn't necessarily an NSMutableSet instance per se, but a proxy wherein any changes you make to that object are reflected in your model's set itself.

This might be why [newRoute setValue:muteSet forKey:@"myCheckpoints"]; is giving you troubles. I find that a better way to think about it is to not have an intermediate object, but to nest calls, eg:

[[newRoute mutableSetValueForKey:@"myCheckpoints"] addObject:newCheckpoint];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM