2013-08-27 62 views
1

我有以下映射:無法獲得RKPathMatcher *的PathMatcher以匹配刪除孤兒對象

RKResponseDescriptor *productionParametersPerEquipment = [RKResponseDescriptor responseDescriptorWithMapping:productionParameterMapping 
                           pathPattern:@"/api/rest/productionparameters/?equipment=:equipment_id" 
                            keyPath:@"objects" 
                           statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

,然後我有:

[objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) { 
    RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"/api/rest/productionparameters/?equipment=:equipment_id"]; 

    NSDictionary *argsDict = nil; 
    BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict]; 
    NSString *productionParameterID; 
    if (match) { 
     productionParameterID = [argsDict objectForKey:@"id"]; 
     NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ProductionParameter"]; 
     fetchRequest.predicate = [NSPredicate predicateWithFormat:@"identifier = %@", @([productionParameterID integerValue])]; // NOTE: Coerced from string to number 
     fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"order" ascending:YES] ]; 
     return fetchRequest; 
    } 

    return nil; 
}]; 

但是,每當我打電話下面, 永遠不會匹配,因此我的孤兒永遠不會被刪除。有任何想法嗎?

[[RKObjectManager sharedManager] getObjectsAtPath:[NSString stringWithFormat:@"/api/rest/productionparameters/?equipment=%@",_equipment.identifier] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 

回答

4

模式匹配器無法匹配URL的查詢字符串中的模式。查詢中的任何內容都需要單獨處理。您應該對URL路徑進行模式匹配,然後在模式匹配時查詢和處理參數。

注意,這種模式匹配限制也適用於刪除處理的範圍之外。

因此,設置您的模式以匹配/api/rest/productionparameters。一旦你得到一場比賽,你想從URL得到query。然後,您可以使用componentsSeparatedByString:將查詢參數拆分爲鍵值對,然後處理每一對,直到找到equipment並提取id

順便提一下,模式永遠不會返回[argsDict objectForKey:@"id"],因爲您將模式參數設置爲equipment_id


類型化的iPad所以檢查,你可能要添加一些檢查,在陣列計數太...:

[objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) { 
    RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"/api/rest/productionparameters"]; 

    NSDictionary *argsDict = nil; 
    BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict]; 
    NSString *productionParameterID; 

    if (match) { 

     NSArray queryItems = [[URL query] componentsSeparatedByString:@"&"]; 

     for (NSString *item in queryItems) { 

      NSArray *keyValPair = [item componentsSeparatedByString:@"="]; 

      if ([keyValPair[0] isEqualToString:@"equipment"]) { 
       productionParameterID = keyValPair[1]; 
       NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ProductionParameter"]; 
       fetchRequest.predicate = [NSPredicate predicateWithFormat:@"identifier = %@", @([productionParameterID integerValue])]; // NOTE: Coerced from string to number 
       fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"order" ascending:YES] ]; 

       return fetchRequest; 
      } 
     } 
    } 

    return nil; 
}]; 
+0

你能否提供更多關於如何做到這一點的信息?我對所有這些URL /匹配的東西都是新手。 – abisson

+0

添加了一些示例代碼 – Wain

+0

我試圖再次得到這個工作......我想知道是否有可能當我們用URL/api/rest/productionparameters(它將映射到ProductionParameters對象)獲取對象時,將addFetchRequestBlock僅比較ProductionParameters對象還是ProductionParameters的propertyMapping? – abisson

1

使用[URL relativeString]代替[URL relativePath],作爲relativePath省略了查詢字符串。

RKPathMatcher matchesPath:tokenizeQueryStrings:YES將插入參數到傳遞的字典中。