북한은 없다
![](https://velog.velcdn.com/images/unisimpson/post/8e2d71c3-8cd7-491b-9210-c251c3b0e4f6/image.png
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="92dp"
android:hint="email"
android:minHeight="48dp"
android:onClick="saveQuote"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/nickName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="11dp"
android:hint="NickName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="read"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nickName" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:text="write"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nickName" />
</androidx.constraintlayout.widget.ConstraintLayout>
import android.content.ContentValues.TAG
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.dreamteam.firestoreprac.databinding.ActivityMainBinding
import com.google.firebase.FirebaseApp
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
FirebaseApp.initializeApp(this)
val db = Firebase.firestore
binding.button2.setOnClickListener {
val eamil = binding.email.text.toString()
val nickName = binding.nickName.text.toString()
val user = hashMapOf(
"email" to eamil,
"nickName" to nickName
)
Toast.makeText(this, "send text", Toast.LENGTH_SHORT).show()
db.collection("hello")
.add(user)
.addOnSuccessListener { documentReference ->
Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.id}")
}
.addOnFailureListener { e ->
Log.w(TAG, "Error adding document", e)
}
}
}
}
val city = hashMapOf(
"name" to "Los Angeles",
"state" to "CA",
"country" to "USA",
)
db.collection("cities").document("LA")
.set(city)
.addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully written!") }
.addOnFailureListener { e -> Log.w(TAG, "Error writing document", e) }
val data = hashMapOf("capital" to true)
db.collection("cities").document("BJ")
.set(data, SetOptions.merge())
val docData = hashMapOf(
"stringExample" to "Hello world!",
"booleanExample" to true,
"numberExample" to 3.14159265,
"dateExample" to Timestamp(Date()),
"listExample" to arrayListOf(1, 2, 3),
"nullExample" to null,
)
val nestedData = hashMapOf(
"a" to 5,
"b" to true,
)
docData["objectExample"] = nestedData
db.collection("data").document("one")
.set(docData)
.addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully written!") }
.addOnFailureListener { e -> Log.w(TAG, "Error writing document", e) }
data class City(
val name: String? = null,
val state: String? = null,
val country: String? = null,
@field:JvmField // use this annotation if your Boolean field is prefixed with 'is'
val isCapital: Boolean? = null,
val population: Long? = null,
val regions: List<String>? = null,
)
val city = City(
"Los Angeles",
"CA",
"USA",
false,
5000000L,
listOf("west_coast", "socal"),
)
db.collection("cities").document("LA").set(city)
db.collection("cities").document("new-city-id").set(data)
// Add a new document with a generated id.
val data = hashMapOf(
"name" to "Tokyo",
"country" to "Japan",
)
db.collection("cities")
.add(data)
.addOnSuccessListener { documentReference ->
Log.d(TAG, "DocumentSnapshot written with ID: ${documentReference.id}")
}
.addOnFailureListener { e ->
Log.w(TAG, "Error adding document", e)
}
val washingtonRef = db.collection("cities").document("DC")
// Set the "isCapital" field of the city 'DC'
washingtonRef
.update("capital", true)
.addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully updated!") }
.addOnFailureListener { e -> Log.w(TAG, "Error updating document", e) }
val docRef = db.collection("objects").document("some-id")
val updates = hashMapOf<String, Any>(
"timestamp" to FieldValue.serverTimestamp(),
)
docRef.update(updates).addOnCompleteListener { }
// Assume the document contains:
// {
// name: "Frank",
// favorites: { food: "Pizza", color: "Blue", subject: "recess" }
// age: 12
// }
//
// To update age and favorite color:
db.collection("users").document("frank")
.update(
mapOf(
"age" to 13,
"favorites.color" to "Red",
),
)
// Create our initial doc
db.collection("users").doc("frank").set({
name: "Frank",
favorites: {
food: "Pizza",
color: "Blue",
subject: "Recess"
},
age: 12
}).then(function() {
console.log("Frank created");
});
// Update the doc without using dot notation.
// Notice the map value for favorites.
db.collection("users").doc("frank").update({
favorites: {
food: "Ice Cream"
}
}).then(function() {
console.log("Frank food updated");
});
/*
Ending State, favorite.color and favorite.subject are no longer present:
/users
/frank
{
name: "Frank",
favorites: {
food: "Ice Cream",
},
age: 12
}
*/
val washingtonRef = db.collection("cities").document("DC")
// Atomically add a new region to the "regions" array field.
washingtonRef.update("regions", FieldValue.arrayUnion("greater_virginia"))
// Atomically remove a region from the "regions" array field.
washingtonRef.update("regions", FieldValue.arrayRemove("east_coast"))
val washingtonRef = db.collection("cities").document("DC")
// Atomically increment the population of the city by 50.
washingtonRef.update("population", FieldValue.increment(50))