首页 > 解决方案 > 放大 DataStore 同步表达式未重新评估

问题描述

我在我的 iOS 应用程序中使用 Amplify DataStore,并且我正在使用以下自定义同步表达式有选择地同步我的数据子集:

func setSyncExpression(idVariable: String) -> [DataStoreSyncExpression] {
    let postSyncExpression = DataStoreSyncExpression.syncExpression(Post.schema) {
        Post.keys.year.ge(2020).and(Post.product.eq("ProductA"))
    }
    let customerSyncExpression = DataStoreSyncExpression.syncExpression(Customer.schema) {
        Customer.keys.customerNumber.eq(idVariable)
    }
    let customSyncEpression = [postSyncExpression, customerSyncExpression]
    return customSyncEpression
}

初始化 Amplify 时使用此表达式:

public var customerId: String = "001A"

func configureAmplify() {
    let customSyncExpressions = setSyncExpression(idVariable: customerId)
    let apiPlugin = AWSAPIPlugin()
    let dataStorePlugin = AWSDataStorePlugin(modelRegistration: AmplifyModels(), 
                                             configuration: .custom(syncExpressions: 
                                                                    customSyncExpressions))    
    do {
        try Amplify.add(plugin: dataStorePlugin)
        try Amplify.add(plugin: apiPlugin)
        try Amplify.configure()
    } catch {
       print("Could not initialize Amplify: \(error)")
    }
}

根据Amplify Docs每当 DataStore 启动时,都会评估同步表达式。为了重新评估您的表达式,您可以执行 Amplify.DataStore.stop() 后跟 Amplify.DataStore.start().

但是,如果我将customerId变量设置为"002A"并重新启动 DataStore,我的数据库中与这个新的 customerNumber 匹配的所有项目都不会同步。

我能够重新同步的唯一方法customerId = "002A"是运行Amplify.DataStore.clear(). 但是,这会清除所有其他模型的所有内容,并强制这些模型也重新同步,这需要很长时间。

如何重新运行选择性同步以从客户模型中提取其他项目?(我知道匹配第一次同步的现有本地数据"001A"也将仍然存在而不运行.clear

或者,有什么方法可以选择性地清除单个模型而不影响其他模型中的本地数据?

标签: iosswiftamazon-web-servicesaws-amplifyaws-appsync

解决方案


推荐阅读