How to use NSManagedObject subclass to save data from NSXMLParser and save it in CoreData?

StackOverflow https://stackoverflow.com/questions/18811857

  •  28-06-2022
  •  | 
  •  

سؤال

I am parsing data like this.

    parserSignIn = [[NSXMLParser alloc]initWithData:_responseData];
    [parserSignIn setDelegate:self];
    [parserSignIn parse];

after that

 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{


   tagName = [[NSString alloc] initWithString:elementName];

   if (parser == parserSignIn) {

    if ([tagName isEqualToString:@"MemberDetail"]) {

        NSLog(@"value is %@", elementName);

    NSManagedObjectContext *managedObjectContext = [database managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"MemberDetails" inManagedObjectContext:managedObjectContext];
    memberDetails = [[MemberDetails alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:managedObjectContext];

       }

   }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    NSLog(@"FOUND %@", string);
    if ([tagName isEqualToString:@"InvalidLogin"]) {

    isSignIn = NO;
   }else{
     NSLog(@"NOT INVALID");
    isSignIn = YES;
    if ([tagName isEqualToString:@"MemberId"]) {

        memberDetails.memberId = string;

    }else if ([tagName isEqualToString:@"FirstName"]){
        memberDetails.firstname = string;


    }else if ([tagName isEqualToString:@"LastName"]){
        memberDetails.lastname = string;

    }
    else if ([tagName isEqualToString:@"Email"]){
        memberDetails.email = string;

    }

    NSLog(@"DATA is %@", memberDetails);
  }
 }

Where MemberDetails.h is

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface MemberDetails : NSManagedObject

@property (nonatomic, retain) NSString * memberId;
@property (nonatomic, retain) NSString * firstname;
@property (nonatomic, retain) NSString * lastname;
@property (nonatomic, retain) NSString * email;


MemberDetails.m is 

@dynamic memberId;
@dynamic firstname;
@dynamic lastname;
@dynamic email;

As I print the data, using NSLog to MemberDetails object

    0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
    email = nil;
    firstname = nil;
    lastname = nil;
    memberId = nil;
    })
    2013-09-15 16:10:33.540 ECP[24094:c07] FOUND 10758
2013-09-15 16:10:33.541 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.541 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
    email = nil;
    firstname = nil;
    lastname = nil;
    memberId = 10758;
})
2013-09-15 16:10:33.543 ECP[24094:c07] FOUND 

2013-09-15 16:10:33.543 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.544 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
    email = nil;
    firstname = nil;
    lastname = nil;
    memberId = "\n  ";
})
2013-09-15 16:10:33.544 ECP[24094:c07] FOUND Amit
2013-09-15 16:10:33.545 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.545 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
    email = nil;
    firstname = Duaan;
    lastname = nil;
    memberId = "\n  ";
})
2013-09-15 16:10:33.545 ECP[24094:c07] FOUND 

2013-09-15 16:10:33.545 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.546 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
    email = nil;
    firstname = "\n  ";
    lastname = nil;
    memberId = "\n  ";
})
2013-09-15 16:10:33.548 ECP[24094:c07] FOUND Metha
2013-09-15 16:10:33.548 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.548 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
    email = nil;
    firstname = "\n  ";
    lastname = Rajput;
    memberId = "\n  ";
})
2013-09-15 16:10:33.548 ECP[24094:c07] FOUND 

2013-09-15 16:10:33.549 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.549 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
    email = nil;
    firstname = "\n  ";
    lastname = "\n  ";
    memberId = "\n  ";
})
2013-09-15 16:10:33.549 ECP[24094:c07] FOUND amit@sevdotcom.ae
2013-09-15 16:10:33.550 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.550 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
    email = "duan@abccompany.ae";
    firstname = "\n  ";
    lastname = "\n  ";
    memberId = "\n  ";
})
2013-09-15 16:10:33.556 ECP[24094:c07] FOUND 
2013-09-15 16:10:33.557 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.557 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
    email = "\n";
    firstname = "\n  ";
    lastname = "\n  ";
    memberId = "\n  ";
})

Why its doing nil, while I am saving data and printing object? and what is \n is saved which is not a part of xml at all?

THANKS

هل كانت مفيدة؟

المحلول

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

This method might not get a whole value - it could be called several times with bits of the content. So, you should have an NSMutableString as a property on your class and append string to it each time it's called. Then use that (and then empty the string) in parser:didEndElement:namespaceURI:qualifiedName:.

Along the same lines, don't save tagName and try to work in parser:foundCharacters:, do most of your work in parser:didEndElement:namespaceURI:qualifiedName: using the element name passed in and the characters that you found and added to your mutable string.

Your log shows that you always have the same instance <MemberDetails: 0x9533320> and that the information you add to it keeps getting overwritten or is just rubbish. I suspect this is to do with the tag you're using, but you haven't shown sample XML so that's hard to judge. It's quite possibly also to do with the fact that most of your processing is in the parser:foundCharacters: method.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top