2014-03-13 129 views
0

我被困在一個與核心數據有關的問題上,一旦我的應用程序加載核心數據數據庫獲取創建。如果證書正確,我有一個用戶實體,通過登錄viewController上的提交按鈕來填充。在下一個視圖中,我從登錄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 

我該如何映射用戶的連接?

+0

你確定你的'currentUser'設置正確嗎? –

回答

0

您正在爲該循環中所列舉的所有連接(用於連接連接)設置相同的用戶實體(即用戶名)。因此,爲什麼,它全部設置爲1.根據您的邏輯正確設置用戶實體。