SRMR
SRMR

Reputation: 3134

Storing data from API in Realm

I'm new to Realm. Right now I have a simple app that pulls news articles from an api. The first view controller shows a list of the article titles, the second view controller shows the article selected from the tableView in a webView.

I'd like to use Realm to store the data from the API, so the tableView will show results even if there is no internet Connection.

I see the tutorials that use Mantle, but I'm trying to do it without that.

This is what I've tried so far, but my use of valueForKey crashed the app, so I commented it out.

- (void)startParsing
{
    NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://images.apple.com/main/rss/hotnews/hotnews.rss#sthash.TyhRD7Zy.dpuf"]];
    [xmlparser setDelegate:self];
    [xmlparser parse];
    if (_marrXMLDataCollection.count != 0) {

        Data *dataRealm = [[Data alloc] init];
        dataRealm.titleR = @"Temporary title";
//        dataRealm.titleR = [_marrXMLDataCollection valueForKey:@"title"];

        RLMRealm *realm = [RLMRealm defaultRealm];
        [realm beginWriteTransaction];
        [realm addObject:dataRealm];
        [realm commitWriteTransaction];

        [self.collectionView reloadData];
    }
}

Any ideas would be great, thanks!

Here's more info if needed:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
{
    if ([elementName isEqualToString:@"rss"]) {
        _marrXMLDataCollection = [[NSMutableArray alloc] init];
    }
    if ([elementName isEqualToString:@"item"]) {
        _mdictXMLPartCollection = [[NSMutableDictionary alloc] init];
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
{
    if (!_mstrXMLStringCollection) {
        _mstrXMLStringCollection = [[NSMutableString alloc] initWithString:string];
    }
    else {
        [_mstrXMLStringCollection appendString:string];
    }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
{
    if ([elementName isEqualToString:@"title"]
        || [elementName isEqualToString:@"pubDate"]
        || [elementName isEqualToString:@"link"]

        ) {
        [_mdictXMLPartCollection setObject:_mstrXMLStringCollection forKey:elementName];
    }
    if ([elementName isEqualToString:@"item"]) {
        [_marrXMLDataCollection addObject:_mdictXMLPartCollection];
    }
    _mstrXMLStringCollection = nil;
}

Upvotes: 0

Views: 778

Answers (1)

TiM
TiM

Reputation: 15991

I'm guessing what you saw was this article on the Realm website about integrating Realm and Mantle.

It's absolutely not necessary to use Realm with any third party parsing libraries. The point of Mantle in that article was to make it easier to convert a JSON response from a REST API into a model object (including things like date strings to NSDate in the process) that could then be handed over to a Realm object.

Your Realm code looks correct here (Assuming that Date has properly been made a subclass of RLMObject), so it would appear there is something wrong with your XML parsing code. NSXMLParser is a rather tricky class to work with since it requires you to build your XML data graph sequentially through delegate callbacks.

For the purpose of parsing XML that's being served from an API (i.e., it would inherently be small, discrete blocks. NSXMLParser is designed for ridiculously huge XML data sets.), I would instead recommend using another XML library that goes through and processes the entire XML set before handing to to you. I've used TBXML in shipping apps before, and there's a Ray Wenderlich article that discusses more of them (Granted, it's pretty out-of-date at this point).

If anyone else knows of any more recent XML libraries that are recommended, please chime in. :)

Upvotes: 1

Related Questions