Apple प्लैटफ़ॉर्म पर ऑफ़लाइन सुविधाएं

Firebase ऐप्लिकेशन तब भी काम करते हैं, जब आपके ऐप्लिकेशन का नेटवर्क कनेक्शन कुछ समय के लिए बंद हो जाता है. इसके अलावा, Firebase में डेटा को स्थानीय तौर पर सेव करने, मौजूदगी को मैनेज करने, और इंतज़ार के समय को मैनेज करने के लिए टूल उपलब्ध हैं.

डिस्क परसिस्टेंस

Firebase ऐप्लिकेशन, नेटवर्क में होने वाली अस्थायी रुकावटों को अपने-आप मैनेज करते हैं. कैश मेमोरी में सेव किया गया डेटा, ऑफ़लाइन होने पर भी उपलब्ध रहता है. साथ ही, इंटरनेट कनेक्शन वापस आने पर, Firebase किसी भी डेटा को फिर से भेजता है.

डिस्क परसिस्टेंस की सुविधा चालू करने पर, आपका ऐप्लिकेशन डेटा को डिवाइस पर सेव करता है. इससे आपका ऐप्लिकेशन ऑफ़लाइन होने पर भी काम करता रहता है. भले ही, उपयोगकर्ता या ऑपरेटिंग सिस्टम ऐप्लिकेशन को रीस्टार्ट कर दे.

सिर्फ़ एक लाइन के कोड की मदद से, डिस्क परसिस्टेंस को चालू किया जा सकता है.

Swift

Note: This Firebase product is not available on the App Clip target.
Database.database().isPersistenceEnabled = true

Objective-C

Note: This Firebase product is not available on the App Clip target.
[FIRDatabase database].persistenceEnabled = YES;

लगातार होने वाला व्यवहार

पर्सिस्टेंस की सुविधा चालू करने पर, Firebase Realtime Database क्लाइंट का जो भी डेटा ऑनलाइन सिंक होता है वह डिस्क में सेव हो जाता है. साथ ही, वह ऑफ़लाइन भी उपलब्ध रहता है. भले ही, उपयोगकर्ता या ऑपरेटिंग सिस्टम ऐप्लिकेशन को रीस्टार्ट करे. इसका मतलब है कि आपका ऐप्लिकेशन, कैश मेमोरी में सेव किए गए लोकल डेटा का इस्तेमाल करके, ऑनलाइन मोड की तरह ही काम करता है. लोकल अपडेट के लिए, लिसनर कॉलबैक ट्रिगर होते रहेंगे.

Firebase Realtime Database क्लाइंट, उन सभी राइट ऑपरेशन की सूची अपने-आप सेव रखता है जो आपके ऐप्लिकेशन के ऑफ़लाइन होने के दौरान किए जाते हैं. पर्सिस्टेंस चालू होने पर, यह सूची डिस्क में भी सेव हो जाती है. इससे, उपयोगकर्ता या ऑपरेटिंग सिस्टम के ऐप्लिकेशन को रीस्टार्ट करने पर, आपके सभी राइट उपलब्ध होते हैं. ऐप्लिकेशन के फिर से कनेक्ट होने पर, सभी कार्रवाइयां Firebase Realtime Database सर्वर पर भेज दी जाती हैं.

अगर आपका ऐप्लिकेशन Firebase से पुष्टि करने की सुविधा का इस्तेमाल करता है, तो Firebase Realtime Database क्लाइंट, ऐप्लिकेशन को रीस्टार्ट करने के दौरान उपयोगकर्ता के पुष्टि करने वाले टोकन को बनाए रखता है. अगर आपका ऐप्लिकेशन ऑफ़लाइन होने के दौरान, पुष्टि करने वाले टोकन की समयसीमा खत्म हो जाती है, तो क्लाइंट डेटा लिखने की प्रोसेस को तब तक रोक देता है, जब तक आपका ऐप्लिकेशन उपयोगकर्ता की फिर से पुष्टि नहीं कर लेता. ऐसा न करने पर, सुरक्षा नियमों की वजह से डेटा लिखने की प्रोसेस पूरी नहीं हो पाती.

डेटा को अप-टू-डेट रखना

Firebase Realtime Database, ऐक्टिव लिसनर के लिए डेटा की एक लोकल कॉपी को सिंक और सेव करता है. इसके अलावा, कुछ जगहों की जानकारी को सिंक करके रखा जा सकता है.

Swift

Note: This Firebase product is not available on the App Clip target.
let scoresRef = Database.database().reference(withPath: "scores")
scoresRef.keepSynced(true)

Objective-C

Note: This Firebase product is not available on the App Clip target.
FIRDatabaseReference *scoresRef = [[FIRDatabase database] referenceWithPath:@"scores"];
[scoresRef keepSynced:YES];

Firebase Realtime Database क्लाइंट, इन जगहों पर मौजूद डेटा को अपने-आप डाउनलोड करता है. साथ ही, रेफ़रंस में कोई ऐक्टिव लिसनर न होने पर भी, डेटा को सिंक रखता है. कोड की इस लाइन का इस्तेमाल करके, सिंक करने की सुविधा को फिर से बंद किया जा सकता है.

Swift

Note: This Firebase product is not available on the App Clip target.
scoresRef.keepSynced(false)

Objective-C

Note: This Firebase product is not available on the App Clip target.
[scoresRef keepSynced:NO];

डिफ़ॉल्ट रूप से, पहले से सिंक किए गए डेटा का 10 एमबी डेटा कैश मेमोरी में सेव होता है. ज़्यादातर ऐप्लिकेशन के लिए, यह तरीका काफ़ी है. अगर कैश मेमोरी का साइज़, कॉन्फ़िगर किए गए साइज़ से ज़्यादा हो जाता है, तो Firebase Realtime Database उस डेटा को हटा देता है जिसका इस्तेमाल हाल ही में सबसे कम किया गया है. सिंक किया गया डेटा, कैश मेमोरी से नहीं मिटाया जाता.

ऑफ़लाइन डेटा के लिए क्वेरी करना

Firebase Realtime Database, क्वेरी से मिले डेटा को सेव करता है, ताकि ऑफ़लाइन होने पर उसका इस्तेमाल किया जा सके. ऑफ़लाइन रहते हुए की गई क्वेरी के लिए, Firebase Realtime Database पहले से लोड किए गए डेटा के लिए काम करता रहता है. अगर अनुरोध किया गया डेटा लोड नहीं हुआ है, तो Firebase Realtime Database लोकल कैश मेमोरी से डेटा लोड करता है. इंटरनेट कनेक्शन फिर से उपलब्ध होने पर, डेटा लोड हो जाता है और क्वेरी के नतीजे दिखते हैं.

उदाहरण के लिए, यह कोड स्कोर के Firebase Realtime Database में मौजूद आखिरी चार आइटम के लिए क्वेरी करता है

Swift

Note: This Firebase product is not available on the App Clip target.
let scoresRef = Database.database().reference(withPath: "scores")
scoresRef.queryOrderedByValue().queryLimited(toLast: 4).observe(.childAdded) { snapshot in
  print("The \(snapshot.key) dinosaur's score is \(snapshot.value ?? "null")")
}

Objective-C

Note: This Firebase product is not available on the App Clip target.
FIRDatabaseReference *scoresRef = [[FIRDatabase database] referenceWithPath:@"scores"];
[[[scoresRef queryOrderedByValue] queryLimitedToLast:4]
    observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) {
      NSLog(@"The %@ dinosaur's score is %@", snapshot.key, snapshot.value);
    }];

मान लें कि उपयोगकर्ता का इंटरनेट कनेक्शन बंद हो जाता है, वह ऑफ़लाइन हो जाता है, और ऐप्लिकेशन को रीस्टार्ट करता है. ऑफ़लाइन रहते हुए, ऐप्लिकेशन उसी जगह से आखिरी दो आइटम के लिए क्वेरी करता है. यह क्वेरी, आखिरी के दो आइटम दिखाती है, क्योंकि ऐप्लिकेशन ने ऊपर दी गई क्वेरी में सभी चार आइटम लोड किए थे.

Swift

Note: This Firebase product is not available on the App Clip target.
scoresRef.queryOrderedByValue().queryLimited(toLast: 2).observe(.childAdded) { snapshot in
  print("The \(snapshot.key) dinosaur's score is \(snapshot.value ?? "null")")
}

Objective-C

Note: This Firebase product is not available on the App Clip target.
[[[scoresRef queryOrderedByValue] queryLimitedToLast:2]
    observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) {
      NSLog(@"The %@ dinosaur's score is %@", snapshot.key, snapshot.value);
    }];

पिछले उदाहरण में, Firebase Realtime Database क्लाइंट, सेव की गई कैश मेमोरी का इस्तेमाल करके, सबसे ज़्यादा स्कोर करने वाले दो डायनासोर के लिए 'चाइल्ड जोड़ा गया' इवेंट जनरेट करता है. हालांकि, यह 'वैल्यू' इवेंट को ट्रिगर नहीं करेगा, क्योंकि ऐप्लिकेशन ने ऑनलाइन रहते हुए उस क्वेरी को कभी भी लागू नहीं किया है.

अगर ऐप्लिकेशन ऑफ़लाइन रहते हुए आखिरी छह आइटम का अनुरोध करता है, तो उसे कैश मेमोरी में सेव किए गए चार आइटम के लिए, 'चाइल्ड जोड़ा गया' इवेंट तुरंत मिल जाएंगे. जब डिवाइस वापस ऑनलाइन आता है, तो Firebase Realtime Database क्लाइंट, सर्वर के साथ सिंक हो जाता है. साथ ही, ऐप्लिकेशन के लिए आखिरी दो 'child added' और 'value' इवेंट पाता है.

लेन-देन को ऑफ़लाइन मैनेज करना

ऐप्लिकेशन के ऑफ़लाइन होने पर किए गए लेन-देन, सूची में जोड़ दिए जाते हैं. ऐप्लिकेशन के फिर से इंटरनेट कनेक्शन होने पर, लेन-देन की जानकारी Realtime Database सर्वर पर भेज दी जाती है.

मौजूदगी मैनेज करना

रीयलटाइम ऐप्लिकेशन में, अक्सर यह पता लगाना मददगार होता है कि क्लाइंट कब कनेक्ट और डिसकनेक्ट होते हैं. उदाहरण के लिए, जब किसी उपयोगकर्ता का क्लाइंट डिसकनेक्ट हो जाता है, तो हो सकता है कि आप उसे 'ऑफ़लाइन' के तौर पर मार्क करना चाहें.

Firebase डेटाबेस क्लाइंट, आसान प्रिमिटिव उपलब्ध कराते हैं. इनका इस्तेमाल करके, डेटाबेस में डेटा लिखा जा सकता है. ऐसा तब किया जा सकता है, जब कोई क्लाइंट Firebase डेटाबेस सर्वर से डिसकनेक्ट हो जाता है. ये अपडेट तब भी होते हैं, जब क्लाइंट सही तरीके से डिसकनेक्ट हो या न हो. इसलिए, डेटा को मिटाने के लिए इन पर भरोसा किया जा सकता है. भले ही, कनेक्शन टूट जाए या क्लाइंट क्रैश हो जाए. डिसकनेक्ट होने पर, डेटा सेव करने से जुड़े सभी काम किए जा सकते हैं. जैसे, डेटा सेट करना, अपडेट करना, और हटाना.

यहां onDisconnect प्रिमिटिव का इस्तेमाल करके, डिसकनेक्ट होने पर डेटा लिखने का एक आसान उदाहरण दिया गया है:

Swift

Note: This Firebase product is not available on the App Clip target.
let presenceRef = Database.database().reference(withPath: "disconnectmessage");
// Write a string when this client loses connection
presenceRef.onDisconnectSetValue("I disconnected!")

Objective-C

Note: This Firebase product is not available on the App Clip target.
FIRDatabaseReference *presenceRef = [[FIRDatabase database] referenceWithPath:@"disconnectmessage"];
// Write a string when this client loses connection
[presenceRef onDisconnectSetValue:@"I disconnected!"];

onDisconnect के काम करने का तरीका

onDisconnect() ऑपरेशन सेट अप करने पर, वह ऑपरेशन Firebase Realtime Database सर्वर पर मौजूद रहता है. सर्वर, सुरक्षा की जांच करता है, ताकि यह पक्का किया जा सके कि उपयोगकर्ता, अनुरोध किए गए राइट इवेंट को पूरा कर सकता है. साथ ही, अगर यह अमान्य है, तो आपके ऐप्लिकेशन को इसकी जानकारी देता है. इसके बाद, सर्वर कनेक्शन को मॉनिटर करता है. अगर किसी भी समय कनेक्शन का टाइम आउट हो जाता है या Realtime Database क्लाइंट उसे बंद कर देता है, तो सर्वर दूसरी बार सुरक्षा की जांच करता है. इससे यह पक्का किया जाता है कि कार्रवाई अब भी मान्य है. इसके बाद, वह इवेंट को ट्रिगर करता है.

आपका ऐप्लिकेशन, डेटा सेव करने की प्रोसेस पर कॉलबैक का इस्तेमाल कर सकता है, ताकि यह पक्का किया जा सके कि onDisconnect को सही तरीके से अटैच किया गया है:

Swift

Note: This Firebase product is not available on the App Clip target.
presenceRef.onDisconnectRemoveValue { error, reference in
  if let error = error {
    print("Could not establish onDisconnect event: \(error)")
  }
}

Objective-C

Note: This Firebase product is not available on the App Clip target.
[presenceRef onDisconnectRemoveValueWithCompletionBlock:^(NSError *error, FIRDatabaseReference *reference) {
  if (error != nil) {
    NSLog(@"Could not establish onDisconnect event: %@", error);
  }
}];

onDisconnect इवेंट को रद्द करने के लिए, .cancel() पर भी कॉल किया जा सकता है:

Swift

Note: This Firebase product is not available on the App Clip target.
presenceRef.onDisconnectSetValue("I disconnected")
// some time later when we change our minds
presenceRef.cancelDisconnectOperations()

Objective-C

Note: This Firebase product is not available on the App Clip target.
[presenceRef onDisconnectSetValue:@"I disconnected"];
// some time later when we change our minds
[presenceRef cancelDisconnectOperations];

कनेक्शन की स्थिति का पता लगाना

मौजूदगी से जुड़ी कई सुविधाओं के लिए, आपके ऐप्लिकेशन को यह पता होना चाहिए कि वह कब ऑनलाइन या ऑफ़लाइन है. Firebase Realtime Database /.info/connected पर एक खास जगह उपलब्ध कराता है. Firebase Realtime Database क्लाइंट के कनेक्शन की स्थिति में बदलाव होने पर, यह जगह हर बार अपडेट होती है. यहां एक उदाहरण दिया गया है:

Swift

Note: This Firebase product is not available on the App Clip target.
let connectedRef = Database.database().reference(withPath: ".info/connected")
connectedRef.observe(.value, with: { snapshot in
  if snapshot.value as? Bool ?? false {
    print("Connected")
  } else {
    print("Not connected")
  }
})

Objective-C

Note: This Firebase product is not available on the App Clip target.
FIRDatabaseReference *connectedRef = [[FIRDatabase database] referenceWithPath:@".info/connected"];
[connectedRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  if([snapshot.value boolValue]) {
    NSLog(@"connected");
  } else {
    NSLog(@"not connected");
  }
}];

/.info/connected एक बूलियन वैल्यू है, जो Realtime Database क्लाइंट के बीच सिंक नहीं होती है. ऐसा इसलिए होता है, क्योंकि वैल्यू क्लाइंट की स्थिति पर निर्भर करती है. दूसरे शब्दों में, अगर कोई क्लाइंट /.info/connected को गलत के तौर पर पढ़ता है, तो इसकी कोई गारंटी नहीं है कि कोई दूसरा क्लाइंट भी इसे गलत के तौर पर पढ़ेगा.

इंतज़ार के समय को मैनेज करना

सर्वर टाइमस्टैंप

Firebase Realtime Database सर्वर, सर्वर पर जनरेट किए गए टाइमस्टैंप को डेटा के तौर पर डालने का तरीका उपलब्ध कराते हैं. onDisconnect के साथ इस सुविधा का इस्तेमाल करने पर, यह आसानी से पता लगाया जा सकता है कि Realtime Database क्लाइंट कब डिसकनेक्ट हुआ:

Swift

Note: This Firebase product is not available on the App Clip target.
let userLastOnlineRef = Database.database().reference(withPath: "users/morgan/lastOnline")
userLastOnlineRef.onDisconnectSetValue(ServerValue.timestamp())

Objective-C

Note: This Firebase product is not available on the App Clip target.
FIRDatabaseReference *userLastOnlineRef = [[FIRDatabase database] referenceWithPath:@"users/morgan/lastOnline"];
[userLastOnlineRef onDisconnectSetValue:[FIRServerValue timestamp]];

क्लॉक स्क्यू

firebase.database.ServerValue.TIMESTAMP ज़्यादा सटीक है और पढ़ने/लिखने की ज़्यादातर कार्रवाइयों के लिए बेहतर है. हालांकि, कभी-कभी Firebase Realtime Database के सर्वर के हिसाब से, क्लाइंट की घड़ी के समय में अंतर का अनुमान लगाना मददगार हो सकता है. /.info/serverTimeOffset जगह की जानकारी के साथ कॉलबैक अटैच किया जा सकता है, ताकि मिलीसेकंड में वह वैल्यू हासिल की जा सके जिसे Firebase Realtime Database क्लाइंट, स्थानीय तौर पर रिपोर्ट किए गए समय (मिलीसेकंड में epoch टाइम) में जोड़ते हैं. इससे सर्वर के समय का अनुमान लगाया जा सकता है. ध्यान दें कि इस ऑफ़सेट की सटीक जानकारी पर, नेटवर्किंग के इंतज़ार के समय का असर पड़ सकता है. इसलिए, यह मुख्य रूप से घड़ी के समय में बड़ी (> 1 सेकंड) गड़बड़ियों का पता लगाने के लिए काम का है.

Swift

Note: This Firebase product is not available on the App Clip target.
let offsetRef = Database.database().reference(withPath: ".info/serverTimeOffset")
offsetRef.observe(.value, with: { snapshot in
  if let offset = snapshot.value as? TimeInterval {
    print("Estimated server time in milliseconds: \(Date().timeIntervalSince1970 * 1000 + offset)")
  }
})

Objective-C

Note: This Firebase product is not available on the App Clip target.
FIRDatabaseReference *offsetRef = [[FIRDatabase database] referenceWithPath:@".info/serverTimeOffset"];
[offsetRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  NSTimeInterval offset = [(NSNumber *)snapshot.value doubleValue];
  NSTimeInterval estimatedServerTimeMs = [[NSDate date] timeIntervalSince1970] * 1000.0 + offset;
  NSLog(@"Estimated server time: %0.3f", estimatedServerTimeMs);
}];

सैंपल प्रेज़ेंस ऐप्लिकेशन

डिसकनेक्ट करने की कार्रवाइयों को कनेक्शन की स्थिति की निगरानी और सर्वर टाइमस्टैंप के साथ जोड़कर, उपयोगकर्ता की मौजूदगी का सिस्टम बनाया जा सकता है. इस सिस्टम में, हर उपयोगकर्ता डेटाबेस की किसी जगह पर डेटा सेव करता है. इससे यह पता चलता है कि Realtime Database क्लाइंट ऑनलाइन है या नहीं. क्लाइंट, ऑनलाइन आने पर इस जगह को 'सही है' पर सेट करते हैं. साथ ही, डिसकनेक्ट होने पर टाइमस्टैंप सेट करते हैं. इस टाइमस्टैंप से पता चलता है कि उपयोगकर्ता पिछली बार कब ऑनलाइन था.

ध्यान दें कि उपयोगकर्ता को ऑनलाइन के तौर पर मार्क करने से पहले, आपके ऐप्लिकेशन को डिसकनेक्ट करने की कार्रवाइयों को सूची में जोड़ना चाहिए. इससे, सर्वर को दोनों निर्देश भेजे जाने से पहले, क्लाइंट का इंटरनेट कनेक्शन टूट जाने पर, किसी भी तरह की रेस कंडिशन से बचा जा सकता है.

यहां उपयोगकर्ता की मौजूदगी का पता लगाने वाले सिस्टम का एक आसान उदाहरण दिया गया है:

Swift

Note: This Firebase product is not available on the App Clip target.
// since I can connect from multiple devices, we store each connection instance separately
// any time that connectionsRef's value is null (i.e. has no children) I am offline
let myConnectionsRef = Database.database().reference(withPath: "users/morgan/connections")

// stores the timestamp of my last disconnect (the last time I was seen online)
let lastOnlineRef = Database.database().reference(withPath: "users/morgan/lastOnline")

let connectedRef = Database.database().reference(withPath: ".info/connected")

connectedRef.observe(.value, with: { snapshot in
  // only handle connection established (or I've reconnected after a loss of connection)
  guard snapshot.value as? Bool ?? false else { return }

  // add this device to my connections list
  let con = myConnectionsRef.childByAutoId()

  // when this device disconnects, remove it.
  con.onDisconnectRemoveValue()

  // The onDisconnect() call is before the call to set() itself. This is to avoid a race condition
  // where you set the user's presence to true and the client disconnects before the
  // onDisconnect() operation takes effect, leaving a ghost user.

  // this value could contain info about the device or a timestamp instead of just true
  con.setValue(true)

  // when I disconnect, update the last time I was seen online
  lastOnlineRef.onDisconnectSetValue(ServerValue.timestamp())
})

Objective-C

Note: This Firebase product is not available on the App Clip target.
// since I can connect from multiple devices, we store each connection instance separately
// any time that connectionsRef's value is null (i.e. has no children) I am offline
FIRDatabaseReference *myConnectionsRef = [[FIRDatabase database] referenceWithPath:@"users/morgan/connections"];

// stores the timestamp of my last disconnect (the last time I was seen online)
FIRDatabaseReference *lastOnlineRef = [[FIRDatabase database] referenceWithPath:@"users/morgan/lastOnline"];

FIRDatabaseReference *connectedRef = [[FIRDatabase database] referenceWithPath:@".info/connected"];
[connectedRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  if([snapshot.value boolValue]) {
    // connection established (or I've reconnected after a loss of connection)

    // add this device to my connections list
    FIRDatabaseReference *con = [myConnectionsRef childByAutoId];

    // when this device disconnects, remove it
    [con onDisconnectRemoveValue];

    // The onDisconnect() call is before the call to set() itself. This is to avoid a race condition
    // where you set the user's presence to true and the client disconnects before the
    // onDisconnect() operation takes effect, leaving a ghost user.

    // this value could contain info about the device or a timestamp instead of just true
    [con setValue:@YES];


    // when I disconnect, update the last time I was seen online
    [lastOnlineRef onDisconnectSetValue:[FIRServerValue timestamp]];
  }
}];