使用XPC在macOS中实现多进程共享数据库 [英] implementing multi-process shared DB in macOS using XPC

查看:225
本文介绍了使用XPC在macOS中实现多进程共享数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是开发可在进程之间共享的健壮,一致且持久的数据库,仅列出Windows注册表即可.

在上一个问题上,由于以下原因,我建议不要使用CFPreferences(和NSUserDefaults)

当前版本的macOS很难用一个第二个进程设置的值来更新一个进程中的值,有时甚至是完全拒绝.

或者,我建议使用以下方法:

要让一个进程负责所有共享值,而另一个进程则通过XPC获取/设置这些值.

XPC对我来说是个很新的东西,但是到目前为止,我似乎对每个连接都使用GCD队列,因此,如果有多个进程要求访问同一数据库以进行R/,那么如何保持一致性? W操作(如何强制单个线程执行所有队列中的项目).

此外,我想使该数据库满足ACID要求,如何实现呢?

解决方案

这是我的建议,也是我在应用程序中使用的解决方案.

(1)创建一个命名的XPC服务.

如果需要通过多个应用程序连接服务,则需要命名并注册已启动的服务应用程序.

(XPC使创建仅由您的应用程序使用的匿名服务非常容易,但是从其他应用程序进行连接会变得有些棘手.从.)

请注意,在我的解决方案中,我已经注册了一个已启动的用户代理,因此,这只是继续执行步骤(2)的问题.

(2)添加XPC消息处理程序以获取并设置您要共享的值.

- (void)queryPreferenceForKey:(NSString*)key withReply:(void(^)(id value))reply
{
    reply([[NSUserDefaults standardUserDefaults] objectForKey:key]);
}

- (void)setPreferenceValue:(id)value forKey:(NSString*)key withReply:(void(^)(BOOL changed))reply
{
    BOOL changed = NO;
    id previous = [[DDUserPreferences standardUserDefaults] objectForKey:key];
    if (!OBJECTS_EQUAL(previous,value))
        {
        [[NSUserDefaults standardUserDefaults] setObject:value forKey:key];
        changed = YES;
        }
    reply(changed);
}

(3)没有步骤3.

基本上就是这样. NSUserDefault类是线程安全的,因此没有并发问题,它会自动处理序列化属性值并将其与应用程序的持久性默认.plist文件同步.

注意:由于这是基于NSUserDefaults的,因此value对象必须是属性列表对象(NSStringNSNumberNSArrayNSDictionaryNSDateNSData, ...).参见首选项和设置编程指南" .

My Goal is to develop robust, coherent, and persistent DB that can be shared between processes, just list Windows Registry.

On a previous question I advised not to use CFPreferences (and NSUserDefaults) due to the following reason

Current versions of macOS have a great deal of difficulty, and sometimes outright refuse, to update the values in one process with the values set by a second process.

Alternatively, I advised to use the following approach:

To have one process responsible for all of the shared values and the other processes get/set those values via XPC.

The XPC is quite new to me but from what I've read so far, it seems to use GCD queues for each connection, so how can I keep coherency if there are multiple processes asking to access the same DB for R/W operations (how can I enforce single thread to execute items in all queues).

Furthermore, I'd like to make this DB to fulfill ACID requirements, how can I achieve this ?

解决方案

Here's my suggestion, and the solution I use in my applications.

(1) Create a named XPC service.

If you need to connect with your service from multiple apps, you'll need to name and register your service app with launchd.

(XPC make it pretty easy to create an anonymous service used only by your app, but connecting from other apps gets a little trickier. Start with the Daemons and Services Programming Guide.)

Note that in my solution, I already had a user agent registered with launchd, so this was just a matter of moving on to step (2).

(2) Add XPC message handlers to get and set the values you want to share.

- (void)queryPreferenceForKey:(NSString*)key withReply:(void(^)(id value))reply
{
    reply([[NSUserDefaults standardUserDefaults] objectForKey:key]);
}

- (void)setPreferenceValue:(id)value forKey:(NSString*)key withReply:(void(^)(BOOL changed))reply
{
    BOOL changed = NO;
    id previous = [[DDUserPreferences standardUserDefaults] objectForKey:key];
    if (!OBJECTS_EQUAL(previous,value))
        {
        [[NSUserDefaults standardUserDefaults] setObject:value forKey:key];
        changed = YES;
        }
    reply(changed);
}

(3) There is no step 3.

Basically, that's it. The NSUserDefault class is thread safe, so there are no concurrently issues, and it automatically takes care of serializing the property values and synchronizing them with the app's persistent defaults .plist file.

Note: since this is based on NSUserDefaults, the value objects must be property-list objects (NSString, NSNumber, NSArray, NSDictionary, NSDate, NSData, ...). See Preferences and Settings Programming Guide.

这篇关于使用XPC在macOS中实现多进程共享数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆