Sqlite IMobileServiceSyncTable。其中筛选器为';不行吗?
更新:如果我使用“f40a0cad-608c-4190-a79e-47af918c087a”对Where过滤器进行硬编码,它将返回用户,则其他字符串字段(如名称或FacebookToken)似乎即使在硬编码时也不起作用 我正在使用WindowsAzure.MobileServices 1.3.0-alpha3和WindowsAzure.MobileServices.SQLiteStore 1.0.0-alpha3 我以前做过很多次,没有任何问题,但我有一个奇怪的问题,我的过滤器没有返回任何东西,当我知道它在表中时。这是我的密码:Sqlite IMobileServiceSyncTable。其中筛选器为';不行吗?,sqlite,azure-mobile-services,Sqlite,Azure Mobile Services,更新:如果我使用“f40a0cad-608c-4190-a79e-47af918c087a”对Where过滤器进行硬编码,它将返回用户,则其他字符串字段(如名称或FacebookToken)似乎即使在硬编码时也不起作用 我正在使用WindowsAzure.MobileServices 1.3.0-alpha3和WindowsAzure.MobileServices.SQLiteStore 1.0.0-alpha3 我以前做过很多次,没有任何问题,但我有一个奇怪的问题,我的过滤器没有返回任何东西,
public async Task SyncUserAsync(string facebookToken)
{
await _userTable.PullAsync ();
var user = (await _userTable.Where(x => x.FacebookToken == facebookToken)
.Take (1)
.ToEnumerableAsync ())
.FirstOrDefault ();
var userTwo = await _userTable.LookupAsync ("f40a0cad-608c-4190-a79e-47af918c087a");
if (user != null)
Debug.WriteLine ("Facebook user {0} is synced", facebookToken);
}
此代码将导致一个空的
用户
,但userTwo
查找将返回我要查找的用户。即使我从userTwo
复制FacebookToken
并硬编码Where
过滤器,它也会返回null LookupAsync方法根据对象的id执行查询。如果要使这两个调用(ToEnumerable和Lookup)等效,则需要按如下方式重写它们:
public async Task SyncUserAsync(string facebookToken)
{
await _userTable.PullAsync ();
var facebookToken = "f40a0cad-608c-4190-a79e-47af918c087a";
var user = (await _userTable.Where(x => x.Id == facebookToken)
.Take (1)
.ToEnumerableAsync ())
.FirstOrDefault ();
var userTwo = await _userTable.LookupAsync (facebookToken);
if (user != null)
Debug.WriteLine ("Facebook user {0} is synced", facebookToken);
}
评论后更新
下面是一个例子,它遵循您的确切场景:
服务:
使用Facebook auth配置的移动服务,具有一个表(用户),具有以下插入脚本:
function insert(item, user, request) {
item.facebookToken = user.userId;
request.execute();
}
客户:
Windows应用商店应用程序(也可以是其他平台)。将NuGet引用添加到最新版本的(1.0.0-beta)
MainPage.xaml:
带有两个按钮的空白应用程序:
<Page
x:Class="App3.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App3"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Name="btnPopulateRemote" Content="Populate remote table" Margin="10" FontSize="30"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Click="btnPopulateRemote_Click"/>
<Button Name="btnPullAndSearch" Content="Pull and search locally" Margin="10" FontSize="30"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1"
Click="btnPullAndSearch_Click"/>
<TextBox Name="txtDebug" AcceptsReturn="True" Margin="10" Grid.Row="1" Grid.ColumnSpan="2"/>
</Grid>
</Page>
MainPage.xaml.cs:
使用系统;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.WindowsAzure.MobileServices;
使用Microsoft.WindowsAzure.MobileServices.SQLiteStore;
使用Newtonsoft.Json;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
名称空间App3
{
///
///可以单独使用或在框架内导航到的空页。
///
公共密封部分类主页面:第页
{
公共静态MobileServiceClient MobileService=新的MobileServiceClient(
"https://MOBILE-SERVICE-NAME.azure-mobile.net/",
);
专用常量字符串TestId=“f40a0cad-608c-4190-a79e-47af918c087a”;
私有字符串facebookToken;
公共主页()
{
this.InitializeComponent();
}
私有异步无效btnPopulateRemote\u单击(对象发送方,路由目标)
{
等待初始化为必需();
调试(“填充远程表…”);
尝试
{
var table=MobileService.GetTable();
var user=新用户
{
Id=“f40a0cad-608c-4190-a79e-47af918c087a”,
Name=“约翰·多伊”
};
等待表。插入同步(用户);
调试(“插入:{0}”,用户);
用户=新用户
{
Id=“01234567-89ab-cdef-0123-456789abcdef”,
Name=“简·罗”
};
等待表。插入同步(用户);
调试(“插入:{0}”,用户);
}
捕获(例外情况除外)
{
跟踪误差(ex);
}
调试(“”);
}
私有无效跟踪错误(例外情况除外)
{
调试(“错误:{0}-{1}”,例如GetType().Name,例如Message);
异常内部=ex.InnerException;
while(内部!=null)
{
调试(“{0}-{1}”,inner.GetType().Name,inner.Message);
inner=inner.InnerException;
}
}
专用异步任务初始化IfNeccessary()
{
if(MobileService.CurrentUser==null)
{
等待MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook);
调试(“以{0}的身份登录”,MobileService.CurrentUser.UserId);
this.facebookToken=MobileService.CurrentUser.UserId;
调试(“将此值用于FacebookToken:{0}”,this.FacebookToken);
调试(“”);
调试(“创建本地存储”);
var store=new mobileseservicesqlitestore(“file.db”);
store.DefineTable();
等待MobileService.SyncContext.InitializeAsync(存储);
调试(“初始化同步上下文/本地存储”);
调试(“”);
}
}
私有异步无效btnPullAndSearch_单击(对象发送方,路由目标)
{
等待初始化为必需();
调试(“在本地提取数据并搜索…”);
尝试
{
var_usersTable=MobileService.GetSyncTable();
wait_usersTable.PullAsync();
调试(“将数据拉入本地表”);
var itemsInLocalTable=(wait _usersTable.ReadAsync()).Count();
调试(“本地表中有{0}个项目”,itemsInLocalTable);
var user=(wait _usersTable.Where(x=>x.FacebookToken==FacebookToken)
.采取(1)
.ToEnumerableAsync())
.FirstOrDefault();
调试(“检索到的用户:{0}”,用户);
var userTwo=await\u usersTable.LookupAsync(TestId);
调试(“用户二:{0}”,用户二);
}
捕获(例外情况除外)
{
跟踪误差(ex);
}
调试(“”);
}
私有void调试(字符串文本,参数对象[]args)
{
如果(args!=null&&args.Length>0)text=string.Format(text,args);
this.txtDebug.Text=this.txtDebug.Text+Text+Environment.NewLine;
}
}
公共类用户
{
[JsonProperty(“id”)]
公共字符串Id{get;set
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.MobileServices;
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
using Newtonsoft.Json;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App3
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public static MobileServiceClient MobileService = new MobileServiceClient(
"https://MOBILE-SERVICE-NAME.azure-mobile.net/",
<<YOUR-APP-KEY-GOES-HERE>>
);
private const string TestId = "f40a0cad-608c-4190-a79e-47af918c087a";
private string facebookToken;
public MainPage()
{
this.InitializeComponent();
}
private async void btnPopulateRemote_Click(object sender, RoutedEventArgs e)
{
await InitializeIfNecessary();
Debug("Populating the remote table...");
try
{
var table = MobileService.GetTable<User>();
var user = new User
{
Id = "f40a0cad-608c-4190-a79e-47af918c087a",
Name = "John Doe"
};
await table.InsertAsync(user);
Debug("Inserted: {0}", user);
user = new User
{
Id = "01234567-89ab-cdef-0123-456789abcdef",
Name = "Jane Roe"
};
await table.InsertAsync(user);
Debug("Inserted: {0}", user);
}
catch (Exception ex)
{
TraceError(ex);
}
Debug("");
}
private void TraceError(Exception ex)
{
Debug("Error: {0} - {1}", ex.GetType().Name, ex.Message);
Exception inner = ex.InnerException;
while (inner != null)
{
Debug(" {0} - {1}", inner.GetType().Name, inner.Message);
inner = inner.InnerException;
}
}
private async Task InitializeIfNecessary()
{
if (MobileService.CurrentUser == null)
{
await MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook);
Debug("Logged in as {0}", MobileService.CurrentUser.UserId);
this.facebookToken = MobileService.CurrentUser.UserId;
Debug("Using this value for the FacebookToken: {0}", this.facebookToken);
Debug("");
Debug("Creating the local store");
var store = new MobileServiceSQLiteStore("file.db");
store.DefineTable<User>();
await MobileService.SyncContext.InitializeAsync(store);
Debug("Initialized the sync context / local store");
Debug("");
}
}
private async void btnPullAndSearch_Click(object sender, RoutedEventArgs e)
{
await InitializeIfNecessary();
Debug("Pulling data locally and searching...");
try
{
var _usersTable = MobileService.GetSyncTable<User>();
await _usersTable.PullAsync();
Debug("Pulled data into local table");
var itemsInLocalTable = (await _usersTable.ReadAsync()).Count();
Debug("There are {0} items in the local table", itemsInLocalTable);
var user = (await _usersTable.Where(x => x.FacebookToken == facebookToken)
.Take(1)
.ToEnumerableAsync())
.FirstOrDefault();
Debug("User retrieved: {0}", user);
var userTwo = await _usersTable.LookupAsync(TestId);
Debug("User two: {0}", userTwo);
}
catch (Exception ex)
{
TraceError(ex);
}
Debug("");
}
private void Debug(string text, params object[] args)
{
if (args != null && args.Length > 0) text = string.Format(text, args);
this.txtDebug.Text = this.txtDebug.Text + text + Environment.NewLine;
}
}
public class User
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("facebookToken")]
public string FacebookToken { get; set; }
public override string ToString()
{
return string.Format("User[{0}, {1}, {2}]", Id, Name, FacebookToken);
}
}
}