【问题标题】:Is there a workaround to avoid error when saving PKTItem containing a 'Calculation' field?保存包含“计算”字段的 PKTItem 时是否有避免错误的解决方法?
【发布时间】:2017-01-10 16:58:44
【问题描述】:

如果PKTItem 包含“计算”字段,则save() 失败:

item["owner"] = myProfile
item.save()
.onSuccess {(savedItem : PKTItem!) in
    print("new owner is me: \(myProfile.name)")
}
.onError {error in
    print("Error: \(error.localizedDescription)")
}

结果是Error: Values cannot be set directly for field with id 126020899(126020899是计算字段)。

但是,如果不存在“计算”字段,则代码会按预期运行,并且 "owner" 字段会更新。

是否根本不可能保存包含“计算”字段的项目?如果没有,我该怎么做?

[编辑] 我真正需要的是一种方法来保存对单个字段的更改,而不是在整个 PKTitem 实例上调用 save()。这对我来说最有意义,因为我真的只需要更新一个字段。需要明确的是,我要更新的字段不是“计算”字段。我要更改的字段 value 是 PKTProfile 实例,它存在于还包含多个“计算”字段的 PKTItem 实例中。如果由我决定,我会去掉“计算”字段,但它们目前是我们工作流程的重要组成部分。

注意:我的示例基于PodioPlatformKit SDK 中给出的示例。虽然 PodioPlatformKit 已贬值,但这种药水在 PodioKit 中是相同的。我只使用它是因为它在 Swift 而不是 Objective-C 中给出了示例。等效的 Objective-C 示例是 here.

【问题讨论】:

    标签: objective-c swift xcode podio


    【解决方案1】:

    我想出的解决方法很简单。我更改了 PKTItem.m 文件中的save 方法,如下所示:

    - (PKTAsyncTask *)save {
      __block PKTAsyncTask *task = nil;
    
      PKTClient *client = [PKTClient currentClient];
    
      [client performBlock:^{
    task = [[PKTApp fetchAppWithID:self.appID] pipe:^PKTAsyncTask *(PKTApp *app) {
      __block PKTAsyncTask *saveTask = nil;
    
      NSArray *itemFields = [self allFieldsToSaveForApp:app];
    
      // Filter out all fields of type PKTAppFieldTypeCalculation, which is the 'Calculation' type
      NSMutableArray *mutItemFields = [NSMutableArray new];
        for (PKTItemField* o in itemFields) {
            if ([o type] != PKTAppFieldTypeCalculation) {
                [mutItemFields addObject:o];
            }
        }
        NSArray *filteredItemFields = [mutItemFields copy];
      [client performBlock:^{
        saveTask = [self saveWithItemFields:filteredItemFields];
       }];
    
      return saveTask;
      }];
     }];
       return task;
     }
    

    【讨论】:

    • 很好,我突然遇到了这个问题,不知道为什么,因为我之前一直在编辑和保存项目而没有问题,尽管我连续做了很多事情,所以想知道是否计算字段不知何故变得混乱......
    猜你喜欢
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多