简体   繁体   English

Swift: MongoDB Realm 查询时不同步

[英]Swift: MongoDB Realm is not syncing when queried

I'm not getting the latest data in my MongoDB cluster when I query the realm in swift.当我在 swift 中查询 realm 时,我没有在我的 MongoDB 集群中获得最新数据。 I initially had about 4000 data in the realm and that synced to mobile but when I increased the data to about 10,000 it does not sync the latest just what was there before.我最初在 realm 中有大约 4000 个数据,并且同步到移动设备,但是当我将数据增加到大约 10,000 个时,它并没有同步最新的数据。 I noticed this print out when the app starts up.我注意到应用程序启动时会打印出来。

Sync: Connection[1]: Session[1]: client_reset_config = false, Realm exists = true, async open = false, client reset = false.同步:连接 [1]:会话 [1]:client_reset_config = false,Realm 存在 = true,异步打开 = false,客户端重置 = false。

this is how I try to sync when the starts up这就是我在启动时尝试同步的方式

struct AppRootView: View {
@State var homeLink = false // <- add here
@State var loginLink = false
@State private var selection: String? = nil


var body: some View {
  NavigationView { // <- wrap in the `NavigationView`
    VStack(alignment: .leading) {
        Text("App")
            .bold()
            .font(.largeTitle)
        
        NavigationLink(destination: homeMainView(), tag: "home", selection: $selection) {EmptyView()}
        NavigationLink(destination: LoginView(), tag: "login", selection: $selection) {EmptyView()}
    }

 }
.onAppear(perform: handleSignIn)
.frame(minWidth: 0,
       maxWidth: .infinity,
       minHeight: 0,
       maxHeight: .infinity).background(Color.yellow)
}

func handleSignIn() {
  print("HANDLING SIGNING IN")
  if let _ = app.currentUser() {
    print("USER IS LOGGED IN ALREADY")
    self.handleRealmSync()

    self.selection = "home" // <- activate the `NavigationLink`
} else {
    print("USER NEEDS TO LOGIN")
    self.selection = "login"
    print("not logged in; present sign in/signup view")
 }
 }

 func handleRealmSync(){
 let user = app.currentUser()
 let partitionValue = "store=walmart"

 Realm.asyncOpen(configuration: user!.configuration(partitionValue: partitionValue),

    callback: { (maybeRealm, error) in
        guard error == nil else {
            fatalError("Failed to open realm: \(error!)")
        }
        guard let realm = maybeRealm else {
            fatalError("realm is nil!")
        }
        // realm opened
        print("Realm SYNC IS OPENED")
     })
  }
 }

I get a print out "Realm SYNC IS OPENED" which makes me think it should be but this我打印出“Realm SYNC IS OPENED”,这让我觉得应该是这样,但是这个

Realm exists = true, async open = false Realm 存在 = true,异步打开 = false

appears to be in conflict.似乎有冲突。 Then I checked MongoDB Realm Log on Sync Session.然后我检查了 MongoDB Realm 登录同步 Session。 This is the message这是消息

["Session closed after receiving UNBIND event"] ["收到 UNBIND 事件后会话关闭"]

it shows that the sync is not happening.它表明同步没有发生。 What could be preventing the syncing?什么可能阻止同步? Any help will be greatly appreciated.任何帮助将不胜感激。

You probably only need to make sure the Realm isn't deinitialized.您可能只需要确保Realm没有被取消初始化。

You could create a @State property to store the Realm instance received in the callback:您可以创建一个@State属性来存储在回调中收到的Realm实例:

@State var realm:Realm?

and

 Realm.asyncOpen(configuration: user!.configuration(partitionValue: partitionValue)) { (maybeRealm, error) in
        guard error == nil else {
            fatalError("Failed to open realm: \(error!)")
        }
        guard let realm = maybeRealm else {
            fatalError("realm is nil!")
        }
        // realm opened
        print("Realm SYNC IS OPENED")
        self.realm = maybeRealm
     }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM