Steaphann
Steaphann

Reputation: 2777

Doesn't put data in my core data database

I have a webservice which gives data back in the following order.

    "club": {
       -"trainingen": [

            {
                "date": "12/10/2012",
                "training": [
                    {
                        "type": "Wedstrijd B-kern Bergen-KRC Genk",
                        "time": "19:30"
                    }
                ]
            },
            {
               {

    "date": "16/10/2012",
    "training": [
        {
            "type": "Training",
            "time": "10:00"
        },
        {
            "type": "Training",
            "time": "14:30"
        }
    ]

},
            },

In my core data database I have an entity training. with attribute: date, attribute: type and attribute: time. I created a subclass of this entity and a category to store it into my database. For getting the webservice url I am doing the following.

+ (NSArray *)getTrainingen
{
    NSString *request = [NSString stringWithFormat:@"http://www.krcgenk.be/mobile/json/request/club/type/ipad"];
    return [[self executeGenkFetch:request] valueForKeyPath:@"club.trainingen"];
}

I defined my values at the top like this.

#define TRAINING_TYPE @"training.type"
#define TRAINING_DATE @"date"
#define TRAINING_TIME @"training.time"

In my category I am doing the following.

+ (Training *)trainingWithGenkInfo:(NSDictionary *)genkInfo
            inManagedObjectContext:(NSManagedObjectContext *)context
                         withIndex: (int)index;
{
    Training *training = nil;

    training = [NSEntityDescription insertNewObjectForEntityForName:@"Training"
                                          inManagedObjectContext:context];

    training.type          = [[[genkInfo objectForKey:@"training"] objectAtIndex:index] objectForKey:@"type"];
    training.time          = [[[genkInfo objectForKey:@"training"] objectAtIndex:index] objectForKey:@"time"];
    training.date          = [genkInfo objectForKey:TRAINING_DATE];

    NSLog(@"type: %@", training.type);
    NSLog(@"time: %@", training.time);

    return training;
}

But it keeps giving (null) for the NSLogs above.

Can anybody help?

Kind regards.

EDIT It's also posible that there can be more than one training a day. I tried this but getting an error.

 int trainingIndex = -1;
     for (NSDictionary *genkInfo in trainingen ) {
          trainingIndex++;         
 [Training trainingWithGenkInfo:genkInfo inManagedObjectContext:document.managedObjectContext withIndex:trainingIndex];

                }

Upvotes: 0

Views: 178

Answers (1)

Martin R
Martin R

Reputation: 540075

It seems that you use Key-Value Coding to access the genkInfo dictionary, e.g. with the key path "training.type". In that case you have to use valueForKeyPath: instead of objectForKey:, e.g.

training.type = [genkInfo valueForKeyPath:TRAINING_TYPE];

EDIT: As I see now, training is an array. As far as I know, you cannot access an array via key-value coding (but I am not 100% sure about that). In any case, you can access the value with

training.type = [[[genkInfo objectForKey:@"training"] objectAtIndex:0] objectForKey:@"type"]

EDIT 2: (This refers to your updated code.) You need 2 loops: an outer loop for the trainingen array, and an inner loop for the training array in the dictionary. This should look roughly like this (there may be typos or errors, because I did not actually run this code):

for (NSDictionary *genkInfo in trainingen) {
    NSArray *training = [genkInfo objectForKey:@"training"];
    for (int i = 0; i < training.count; i++) {
        [Training trainingWithGenkInfo:genkInfo
                inManagedObjectContext:document.managedObjectContext
                             withIndex:trainingIndex];
    }
}

This might not be the optimal code, but you should be able to figure out a solution like this.

Remark: If something does not work and you get error messages, then you should add the exact error message to your question, and also locate the point in your code where the message occurs. That makes it easier for others to help you.

Upvotes: 1

Related Questions