核心数据用户添加关系

时间:2014-03-13 05:50:42

标签: ios objective-c core-data

我遇到了一个与核心数据有关的问题,一旦我的应用加载核心数据数据库就会创建。如果凭据正确,我有一个用户实体通过登录viewController上的提交按钮填充。在下一个View中,我从登录viewController获取返回数据并将其提交到另一个URL。这为我提供了JSON返回数据,该数据也存储在数据库中。

我已经附加了我的数据模型,除了用户实体之外,一切正常,我不能让我的生活让合适的用户使用连接内的数据。

datamodel

这些是我的用户 users

每个用户都有两个记录,下面是存储的内容,而不是用户列中的所有1个,应该有两个1和两个2。

table

这是我用来存储数据的代码

  User *username = [User MR_findFirstByAttribute:@"username" withValue:currentUser];
  NSDictionary *parameters = @{@"DeviceKey": username.guid};

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

  //I'm calling all the users with this piece
  NSArray *elements = [User MR_findAll];
  id attributes = [elements  valueForKey:@"username"];

  NSLog(@"user: %@",attributes);
  username.sessionKey =[[operation.responseObject objectForKey:@"Data"]objectForKey:@"sessionId"];

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

   NSMutableArray *connectionArray = [NSMutableArray array];
  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;

    // [user addConnections:connection];
       [connectionArray addObject:connection];

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

      NSMutableArray *categoryArray = [NSMutableArray array];

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


          FCCategory *category = [FCCategory MR_createEntity];
          category.title = title;
          category.unreadCount = unreadCount;


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




          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"];
             // BaseTopic *baseEntity = [A MR_createEntity];
             // baseEntity.title   = title;
              Article *article = [Article MR_createEntity];
              article.link = link;
              article.systemID = systemID;
              article.isRead = isRead;

              [article setValue:title forKey:@"title"];
              article.category = category;

              [itemArray addObject:article];
                NSLog(@"item %@",itemArray);
          }

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

      connectionEntity.user = username;
      [connectionEntity addCategories:[NSSet setWithArray:categoryArray]]; // mapping the items to connection entity

如何将用户映射到连接?

1 个答案:

答案 0 :(得分:0)

您正在为该循环中枚举的所有连接设置相同的用户实体(即用户名)(用于(连接中的连接))。因此,为什么,它都设置为1.根据您的逻辑正确设置用户实体。