我正处于新的Xamarin Forms应用程序的计划阶段。
我希望应用程序首先离线。
由此,我的意思是应该从本地数据存储读取/写入数据。然后,只要有连接(可能是立即或几小时后),数据就会在本地数据存储和云之间同步。
我正在寻找的是标准设计模式。我发现了一些关于这个主题的文章,但是ost是关于专有解决方案的。
This article似乎是一个非常好的开始,但它并不是真正关于离线优先。有没有人知道一些关于适用于Xamarin Forms或至少.Net的离线优先主题的优秀文献。
非常感谢您的时间和答案, 于连
答案 0 :(得分:1)
这是一个非常复杂的问题,但简而言之,我过去的做法是使用共享IService
与共享库中的所有逻辑。
此库位于移动应用和API中,负责处理同步。 api将具有具体实现(对于您的repo),移动应用程序将具有具体实现(对于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
。我建议立即这样做的原因是因为用户希望在任何地方看到这些数据。
答案 1 :(得分:0)
您可以使用Microsoft Azure移动客户端或Realm实现离线/在线数据同步。 Realm提出了一个重要的观点,即他们首先要脱机。虽然Azure移动客户端主要是关于您的主数据库是在云中,能够在本地缓存该数据,以便在您离线时进行CRUD操作,然后在您重新联机后可以与后端同步。
您可以在此处了解有关Realm的更多信息
您可以在此处详细了解如何使用Azure移动客户端