Arun
Arun

Reputation: 644

Core Data Saving and Fetching

I want to import these datas in CoreData framework with save and retrieve.can anyone please tell me how to implement this in CoreData Framework or some reference tutorial and please tell me how to save these datas in Entities.

JSON: {
    Options =     (
    );
    Questions =     (
                {
            AssessmentId = 4;
            QuestionDesc = "Below are five statements that you may agree or disagree with. Using the 1 - 7 scale below, indicate your agreement with each item by placing the appropriate number on the line preceding that item. Please be open and honest in your responding";
            QuestionId = 18;
            QuestionNo = 1;
            QuestionTypeDesc = Rating;
            QuestionTypeId = 3;
        }
    );
    RankingOptions =     (
    );
    RatingOptions =     (
                {
            AnswerId = 1;
            OptionDesc = "In most ways my life is close to my ideal. ";
            OptionId = 1;
            OptionValue = 1;
            QuestionId = 18;
        },
                {
            AnswerId = 2;
            OptionDesc = "The conditions of my life are excellent.";
            OptionId = 2;
            OptionValue = 2;
            QuestionId = 18;
        },
                {
            AnswerId = 3;
            OptionDesc = "I am satisfied with my life.";
            OptionId = 3;
            OptionValue = 3;
            QuestionId = 18;
        },
                {
            AnswerId = 4;
            OptionDesc = "So far I have gotten the important things I want in life.";
            OptionId = 4;
            OptionValue = 4;
            QuestionId = 18;
        },
                {
            AnswerId = 5;
            OptionDesc = "If I could live my life over, I would change almost nothing.";
            OptionId = 5;
            OptionValue = 5;
            QuestionId = 18;
        }
    );
    ResponseDetails =     {
        Msg = "DATA FOUND!";
        ResultStatus = 1;
    };
}

Upvotes: 0

Views: 45

Answers (1)

Jan49
Jan49

Reputation: 1818

First you need to convert these json data into NSDictionary

NSError* error;
NSDictionary* inDictionary = [NSJSONSerialization JSONObjectWithData:responseData
                                                 options:kNilOptions 
                                                   error:&error];

Then save this NSDictionary into core data.

 AppDelegate *sharedDelegate = (AppDelegate *)[[UIApplication   sharedApplication] delegate];
 NSManagedObjectContext *context = [sharedDelegate managedObjectContext];    
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"QestInfo"
                                          inManagedObjectContext:context]; //  Create an Entity in coredata  "QestInfo" (use your entity name)
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:nil];

QestInfo * qestInfo = [NSEntityDescription
                          insertNewObjectForEntityForName:@"ThreadInfo"
                          inManagedObjectContext:context];

for (QestInfo *info in fetchedObjects)
{

    if([[inDictionary allKeys] containsObject:@"userEmail"])
    {
        if([inDictionary valueForKey:@"userEmail"]!=[NSNull null])
        {
            qestInfo. AssessmentId =[inDictionary valueForKey:@"userEmail"];
        }
    }

.
.// Your key here
.
}
NSError *error;
if(![context save:&error]){
    NSLog(@"SAVE ERROR");
}

Also check this http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started tutorial for beginners.

Upvotes: 3

Related Questions