سؤال

I'm building an application that's pullingJSON from a url and using Core Data and Magical Record to store it on the device. The database is being create but the relationships are showing null in the database. The way I am trying to get it to flow is:

  1. The user has connections
  2. The connections has Categories
  3. The Categories has Articles which should call in the title from the Basic Topic.

Below is my Data Model everything seems fine on that part.

Data Model

This is how I am parsing the data and storing it in the entities

[_operationManager POST:@"feeds" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

      id connections = [[operation.responseObject objectForKey:@"Data"]objectForKey:@"connections"];

       username.sessionKey =[[operation.responseObject objectForKey:@"Data"]objectForKey:@"sessionId"];

      id connection;
      for(connection in connections){

          //Get the JSON values
          NSString *contentSystemID = [connection objectForKey:@"contentSystemId"];
          NSString *contentSystemName = [connection objectForKey:@"name"];
          NSString *logoUrl =  [connection objectForKey:@"logo"];
          NSNumber *unreadCount = [connection objectForKey:@"unread_count"];


          NSLog(@"Content System ID = %@",contentSystemID);
          NSLog(@"Content System Name = %@",contentSystemName);
          NSLog(@"Logo URL = %@",logoUrl);
          NSLog(@"Unread Count = %@",unreadCount);


          FCConnection *connectionEntity = [FCConnection MR_createEntity];
          connectionEntity.contentSystemID   = contentSystemID;
          connectionEntity.contentSystemName = contentSystemName;
          connectionEntity.logoUrl = logoUrl;
          connectionEntity.unreadCount = unreadCount;

          id categories = [connection objectForKey:@"categories"];
          for (id cat in categories)
          {
             NSString *title = [cat valueForKey:@"name"];
             NSString *unreadCount = [cat valueForKey:@"unread_count"];
             NSLog(@"title  = %@",title);
             NSLog(@"unread_count  = %@",unreadCount);

              id items = [cat objectForKey:@"items"];
             // NSLog(@"items = %@",items);
              for (id item in items){
                  //
                 // NSLog(@"items = %@",item);
                  NSNumber *isRead = [item valueForKey:@"unread"];
                  NSString *title = [item valueForKey:@"title"];
                  NSString *link = [item valueForKey:@"link"];
                  NSString *systemID = [item valueForKey:@"itemId"];

                  NSLog(@"isRead  = %@",isRead);
                  NSLog(@"title  = %@",title);
                  NSLog(@"link  = %@",link);
                  NSLog(@"systemID  = %@",systemID);

                  BaseTopic *baseEntity = [BaseTopic MR_createEntity];
                  baseEntity.title   = title;

                  Article *article = [Article MR_createEntity];
                  article.link = link;
                  article.systemID = systemID;
                  article.isRead = isRead;

              }

          }
      }

          [MagicalRecord saveUsingCurrentThreadContextWithBlock:nil completion:^(BOOL success, NSError *error) {
              NSLog(@"Data Stored");

 }];
       } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        //Failure
        NSLog(@"Failed to fetch!");

                                         } ];
      }

I know that something in this section is causing my Core Data file to look like the below. As you can see the data is being separated and has multiple rows. There should only be three rows, I'm guessing because the title is in the parent entity"BasicTopic" this issue is happening.

Duplication and seperation  issue

My next issue is that the relationships are not populating in the tables. The below is the rows from FCConnection which has a relationship with user, for some odd reason the row is not being populated with User's pk.

Any help or suggestions will be appreciated.

Relationships issue

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

المحلول

You need to map the entity records to each relationship. If you generate the managed object subclasses for these entities you can see few methods for each relationship. you need to use that to map the entity records.

id connection;
for(connection in connections)
{
    //Get the JSON values
    NSString *contentSystemID = [connection objectForKey:@"contentSystemId"];
    NSString *contentSystemName = [connection objectForKey:@"name"];
    NSString *logoUrl =  [connection objectForKey:@"logo"];
    NSNumber *unreadCount = [connection objectForKey:@"unread_count"];

    FCConnection *connectionEntity = [FCConnection MR_createEntity];
    connectionEntity.contentSystemID   = contentSystemID;
    connectionEntity.contentSystemName = contentSystemName;
    connectionEntity.logoUrl = logoUrl;
    connectionEntity.unreadCount = unreadCount;

    id categories = [connection objectForKey:@"categories"];

    NSMutableArray *categoryArray = [NSMutableArray array];

    for (id cat in categories)
    {
        NSString *title = [cat valueForKey:@"name"];
        NSString *unreadCount = [cat valueForKey:@"unread_count"];

        id items = [cat objectForKey:@"items"];

        BaseTopic *baseEntity = [BaseTopic MR_createEntity];
        baseEntity.title   = title;

FCCatgory *category = [FCCatgory MR_createEntity];

        NSMutableArray *itemArray = [NSMutableArray array];

        for (id item in items)
        {
            NSNumber *isRead = [item valueForKey:@"unread"];
            NSString *title = [item valueForKey:@"title"];
            NSString *link = [item valueForKey:@"link"];
            NSString *systemID = [item valueForKey:@"itemId"];

            Article *article = [Article MR_createEntity];
            article.link = link;
            article.systemID = systemID;
            article.isRead = isRead;

            article.category = category;

            [itemArray addObject:article];
        }

        [category addArticles:[NSSet setWithArray:itemArray]]; // mapping the items to category
        category.connection = connectionEntity;
        [categoryArray addObject:category];
    }

    [connectionEntity addCategories:[NSSet setWithArray:categoryArray]]; // mapping the items to connection entity
}

Hope this will help you

نصائح أخرى

I'm not seeing where you're actually making the object connections in your data import code. You should have something like

[user addConnectionObject:connection]

At the end of each loop, and for every relationship you need to make

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