Writing data into firebase from Android app

Realtime Database में data save करने के लिए आप एक path पर एक reference point करते हैं और setValue call करते हैं, और lists के लिए आप हर नए item के लिए एक unique key generate करने के लिए push इस्तेमाल करते हैं, तो entries कभी एक-दूसरे को overwrite नहीं करतीं।

10 min read · 7 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Cloud में Data Save करना

अब जब आप एक DatabaseReference को एक path पर point कर सकते हैं, आप वहाँ data लिख सकते हैं। जब FestConnect में एक student एक event के लिए register करता है, वह registration Realtime Database में save होनी चाहिए तो यह persist और sync करे।

लिखना ज़्यादातर एक method है, setValue, plus एक important helper, push, एक list में items add करने के लिए बिना उन्हें एक-दूसरे को clobber किए। यह lesson दोनों दिखाता है, और इनके बीच crucial difference। Writes सही करना, especially push-versus-setValue distinction, एक बहुत common data-loss bug रोकता है।

Theory

setValue एक Path पर लिखता है

setValue(data) reference के path पर एक value लिखता है, जो कुछ वहाँ था उसे replace करते हुए। आप एक primitive, एक map, या एक simple object (एक Kotlin data class) लिख सकते हैं।

तो database.getReference("settings").child("theme").setValue("dark") settings/theme पर 'dark' store करता है। यह एक single, known location के लिए perfect है, एक setting, एक profile field। पर सावधान रहिए: चूँकि setValue overwrite करता है, same fixed path पर दो बार लिखना पहली value को replace करता है। यह single items के लिए fine है, पर एक list के लिए wrong है, जहाँ आप entries add करते रहना चाहते हैं। इसके लिए आपको push चाहिए।

Practical

एक Single Value लिखना, और push के साथ एक List में Add करना

val db = FirebaseDatabase.getInstance()

// Single, known location: setValue overwrites
db.getReference("settings").child("theme").setValue("dark")

// A list: push() makes a UNIQUE key per item, so entries never collide
val eventsRef = db.getReference("events")
val event = mapOf("name" to "Robotics", "seats" to 50)
eventsRef.push().setValue(event)     // events/<auto-id>/...
eventsRef.push().setValue(mapOf("name" to "Coding"))  // a different auto-id

Formula

Lists के लिए push, एक Single Item के लिए setValue

यही write rule है जो याद रखनी है। एक fixed path पर setValue इस्तेमाल कीजिए जब वहाँ exactly एक चीज़ हो (एक setting, एक specific record जिसकी key आप जानते हैं)। एक collection में items add करते समय push() इस्तेमाल कीजिए: push एक unique auto-id child key generate करता है, तो हर नए event या registration को अपना slot मिलता है और कुछ भी कुछ overwrite नहीं करता।

यह गलत लीजिए, same list path पर repeatedly setValue call कीजिए, और हर write last को replace करती है, silently data खोते हुए। push() ही है जो एक list को safely grow करने देता है। जब doubt हो, अगर आप एक collection में add कर रहे हैं, push कीजिए।

Quiz

आप Realtime Database में एक 'registrations' list में कई registrations add कर रहे हैं। हर बार same path पर ref.setValue(...) की बजाय ref.push().setValue(...) क्यों इस्तेमाल करें?

  1. push सिर्फ़ setValue लिखने का एक shorter तरीका है
  2. push() हर item के लिए एक unique key generate करता है, तो registrations एक-दूसरे को overwrite नहीं करतीं, जबकि same path पर setValue previous value को replace करता है
  3. setValue Realtime Database के साथ काम नहीं करता
  4. push data encrypt करता है
Show the answer

push() हर item के लिए एक unique key generate करता है, तो registrations एक-दूसरे को overwrite नहीं करतीं, जबकि same path पर setValue previous value को replace करता है

push() हर नए child के लिए एक unique auto-generated key create करता है, तो हर registration अपने slot में store होती है और list items को एक-दूसरे को clobber किए बिना grow कर सकती है। Same fixed path पर repeatedly setValue call करना हर बार previous value को OVERWRITE करता, सिर्फ़ last registration रखते हुए, एक data-loss bug। Option A wrong है: push सिर्फ़ shorthand नहीं है; यह एक नई unique key generate करके behaviour change करता है। Option C false है: setValue fine काम करता है (यह एक single known location के लिए correct tool है)। Option D invented है; push कुछ भी encrypt नहीं करता। Collections के लिए, हर item के लिए एक unique key पाने के लिए push कीजिए; एक single known location के लिए, setValue कीजिए।

Think first

push() एक Central Counter के बिना Unique Keys कैसे Guarantee करता है?

कई devices एक साथ push कर सकते हैं। push() दो items को same key मिलने से कैसे बचाता है, बिना एक central id server के? फिर tap कीजिए।

Show the answer

क्योंकि push() ऐसी keys generate करता है जो TIME-ORDERED और RANDOM दोनों हैं, इस तरह engineered कि कई clients एक साथ push करने पर भी बिना एक central counter के through coordinate किए अलग keys produce करें। एक naive approach, 'next number के लिए एक server से पूछिए', एक bottleneck होगा और offline या heavy concurrency में struggle करेगा। इसके बजाय, एक Firebase push key दो ingredients से build होती है: एक timestamp (तो keys उस order में sort होती हैं जिसमें items add हुए, जो chronological lists के लिए handy है) और random bits का एक chunk। Timestamp portion अलग moments की keys को naturally distinct और ordered बनाता है; random portion SAME millisecond में generated keys को, अलग devices पर भी, still almost certainly अलग बनाता है, क्योंकि दो random components के collide होने की odds astronomically small हैं। Crucially, इसका मतलब है एक client entirely अपने-आप एक valid, unique key generate कर सकता है, instantly, यहाँ तक कि OFFLINE रहते हुए भी, बिना server को round trip के, और confident रह सकता है यह दूसरे clients द्वारा बनाई गई keys से clash नहीं करेगी। यह एक real-time, multi-device, offline-capable database के लिए एक बड़ी बात है: writes को कभी एक central authority के ids hand out करने का wait नहीं करना पड़ता। तो push() आपको unique, time-sortable keys clever local generation से देता है central coordination की बजाय, यही exactly वजह है यह growing shared lists के लिए right tool है। Time plus randomness, locally generated, keys देते हैं जो बिना एक coordinator के unique हैं।

Summary

Key takeaways

  • Realtime Database में लिखने के लिए, एक path पर एक reference point कीजिए और setValue call कीजिए।
  • setValue(data) उस path पर एक value लिखता है, जो कुछ वहाँ था उसे replace करते हुए; इसे एक single, known location के लिए इस्तेमाल कीजिए।
  • Lists/collections के लिए, एक unique auto-id child key generate करने के लिए push() इस्तेमाल कीजिए, फिर इस पर setValue कीजिए।
  • push() एक list को items एक-दूसरे को overwrite किए बिना grow करने देता है; एक fixed path पर setValue previous value replace कर देता।
  • Rule: एक collection में add करने के लिए push, एक single known item के लिए setValue।
  • Writes asynchronous हैं (ये एक Task return करते हैं जिसे आप optionally handle कर सकते हैं); आप primitives, maps, या simple objects लिख सकते हैं।
  • Memory hook: setValue एक path पर लिखता/overwrite करता है, push हर नए list item के लिए एक unique key बनाता है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Firebase Data storage

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Writing data into firebase from Android app · Advance Mobile Application Development - II (Major-15-02) · Gri-Learn