gpt4 book ai didi

database - Xamarin.Forms 和离线优先

转载 作者:搜寻专家 更新时间:2023-10-30 20:35:06 25 4
gpt4 key购买 nike

我正处于新 Xamarin Forms 应用的规划阶段。

我希望应用程序先离线

我的意思是数据应该从本地数据存储中读取/写入。然后,一旦建立连接(可能立即或几小时后),数据就会在本地数据存储和云之间同步。

我正在寻找的是标准设计模式。我找到了几篇关于这个主题的文章,但大多数都是关于专有解决方案的。

This article似乎是一个很好的开始,但这并不是真正的离线优先。有没有人知道一些关于适用于 Xamarin Forms 或至少适用于 .Net 的离线优先主题的优秀文献。

非常感谢您的时间和答复,朱利安

最佳答案

这是一个非常复杂的问题,但简而言之,我过去的做法是使用共享的 IService 和共享库中的所有逻辑。

此库位于移动应用程序和 API 中,负责处理同步。 api 将具有具体实现(对您的存储库),移动应用程序将具有具体实现(对 Sqlite)以及使用 HttpClient 的代理实现。

关于 REPO,您将有一个用于移动设备,一个用于服务器,并再次共享相同的 IRepo

注意:对所有标识符使用 GUID,永远不要自动递增,否则会发生冲突

示例

// everything in here lives in a shared library that the server and mobile have access to.

public interface IUserRepository {
// shared methods to talk to data
}

public interface IUserService {
UserDto GetUserById(Guid userId);
IList<UserDto> FindUsers(int skip, int take);
void DeleteUserById(Guid userId);
// etc
}

public class UserService : IUserService {
public UserService (IUserRepository userRepo) {
_userRepo = userRepo;
}
}

public class UserProxyService : IUserService {
public UserProxyService (HttpClient httpClient) {
_httpClient = httpClient; // don't dispose... make singleton
}
}
// server implementation of the repo
public namespace MyApp.Api.Data {
public class UserRepository : IUserRepository {
// sql server calls (EF maybe)
}
}
// mobile implementation of the repo
public namespace MyApp.Mobile.Data { /* (portable) */
public class UserRepository : IUserRepository {
// sqlite calls on the phone
}
}

至于处理同步。我的建议是在可能的情况下立即推送您的更改,并且可能在之前的尝试因网络问题而失败时 OnResume。我建议立即执行此操作的原因是用户希望随处都能看到该数据。

关于database - Xamarin.Forms 和离线优先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43983896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com