2014-03-25 75 views
0

是否有可能使restkit中的映射相互依賴。我有以下JSON結構:Restkit映射依賴關係

{ 
    "IdList": [ 
     "1", 
     "2", 
     "3", 
     "4", 
     "5", 
     "6", 
     "7", 
     "8", 
     "9", 
     "10" 
    ], 
    「Persons」: [ 
     { 
      "Id": 2, 
      "Name": 「James」, 
      "Description": 「Tall」, 
      "Items": [ 
       { 
        "Id": 1051, 
        "Name": 「Hat」, 
        "Description": "", 
        「Accesories」: [] 
       } 
      ] 
     } 
    ] 
} 

我有2個響應描述符,以鑽入陣列IDLIST &人員。

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider personsMapping] method:RKRequestMethodGET pathPattern:nil keyPath:@"Persons"statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 
[self addResponseDescriptor:responseDescriptor]; 

RKResponseDescriptor *IdsResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider IdsMapping] method:RKRequestMethodGET pathPattern:nil keyPath:@"IdList" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 
[self addResponseDescriptor:IdsResponseDescriptor]; 

我的映射代碼:

+ (RKDynamicMapping *)IdsMapping { 

    RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Ids" inManagedObjectStore:[[CoreDataManager sharedInstance] objectStore]]; 

    mapping.discardsInvalidObjectsOnInsert = YES; 

    [mapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"theId"]]; 

    RKDynamicMapping *idsMapping = [RKDynamicMapping new]; 

    [dynamicTableIdsMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) { 

     if (!representation) { 

      return nil; 

     } else { 

      return mapping; 

     } 

     return nil; 

    }]; 

    return dynamicTableIdsMapping; 
} 

+ (RKDynamicMapping *)personsMapping { 

    RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Persons" inManagedObjectStore:[[CoreDataManager sharedInstance] objectStore]]; 

    mapping.assignsNilForMissingRelationships = YES; 
    mapping.assignsDefaultValueForMissingAttributes = YES; 
    mapping.discardsInvalidObjectsOnInsert = YES; 

    [mapping addAttributeMappingsFromDictionary:@{ 
     @"Id": @"remoteID", 
     @"Name": @"name" 
    }]; 

    mapping.identificationAttributes = @[ @"remoteID" ]; 

    [mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Items" 
                        toKeyPath:@"products" 
                       withMapping:[MappingProvider itemsMapping]]]; 
    RKDynamicMapping * dynamicMenuMapping = [RKDynamicMapping new]; 


    [dynamicMenuMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) { 

     if (!representation) { 

      return nil; 

     } else { 

      NSArray *categoryArray = [representation valueForKey:@"Items"]; 

      if (!categoryArray || !categoryArray.count) { 

       return nil; 

      } else { 

       return mapping; 

      } 
     } 

     return nil; 

    }]; 

    return dynamicMenuMapping; 
} 

我將如何管理對人數組首先映射,然後IDLIST?如果個人包含值,我只需要映射idList數組。我想這樣做,因爲只有當用戶存在Persons中的數據時,用戶纔會出現在新視圖中,因此也是idList。因此,如果persons數組爲空,那麼映射IdList並不是必須的。

回答

1

您需要使用帶有零鍵路徑的單個響應描述符。該響應描述符的映射將是一個動態映射,它檢查內容並返回一個處理兩個子部分的映射(因此您需要一個容器類)或零(放棄映射過程)。

+0

謝謝你的回答。我不確定你的意思是我的容器類嗎?你能否詳細說明一下?謝謝。 – 7c9d6b001a87e497d6b96fbd4c6fdf

+0

要做你所要求的,你需要1個響應描述符。這自然會映射到1個類的類型,但是您有2個(嵌套的)。所以你需要創建一個與其他類有關係的新類,以方便處理。 – Wain

+0

在映射完成後更改服務器響應或刪除一些數據可能會更簡單... – Wain