I am working on a Mac app written in Swift. In order to execute some code on login, I created a helper app. The helper app needs to access data from the main app. Thus, I enabled "App Groups" for both main and helper app, to share data via UserDefaults
. So far, so good.
我正在使用Swift编写的Mac应用程序。为了在登录时执行一些代码,我创建了一个帮助应用程序。帮助应用程序需要从主应用程序访问数据。因此,我为主应用程序和帮助应用程序启用了“应用程序组”,以通过UserDefaults共享数据。到现在为止还挺好。
I started to create the model for my data in the main app and just realized that I have no clue how to teach the helper app about the model.
我开始在主应用程序中为我的数据创建模型,并且意识到我不知道如何教授关于模型的帮助应用程序。
So, how do I implement my model into both apps?
那么,我如何在两个应用程序中实现我的模型?
1 个解决方案
#1
1
You can only share raw data between two apps, not application logic.
您只能在两个应用之间共享原始数据,而不是应用逻辑。
So I think the best solution for your case is following: Extract your model sourcecode into a framework and embed it into both applications. The model only uses the shared raw data and should be stateless.
因此,我认为针对您的案例的最佳解决方案如下:将模型源代码提取到框架中并将其嵌入到两个应用程序中。该模型仅使用共享原始数据,并且应该是无状态的。
But note: When changing the model you have to ensure that both apps will be updated or you might get a compatibility issue. So maybe you should version your model and check the version first before using it.
但请注意:更改模型时,您必须确保更新这两个应用程序,否则可能会出现兼容性问题。所以也许您应该在使用之前对模型进行版本控制并首先检查版本。
#1
1
You can only share raw data between two apps, not application logic.
您只能在两个应用之间共享原始数据,而不是应用逻辑。
So I think the best solution for your case is following: Extract your model sourcecode into a framework and embed it into both applications. The model only uses the shared raw data and should be stateless.
因此,我认为针对您的案例的最佳解决方案如下:将模型源代码提取到框架中并将其嵌入到两个应用程序中。该模型仅使用共享原始数据,并且应该是无状态的。
But note: When changing the model you have to ensure that both apps will be updated or you might get a compatibility issue. So maybe you should version your model and check the version first before using it.
但请注意:更改模型时,您必须确保更新这两个应用程序,否则可能会出现兼容性问题。所以也许您应该在使用之前对模型进行版本控制并首先检查版本。