0Pricing
Indie Hacker Mobile Apps · บทเรียน

การเก็บข้อมูลภายในอุปกรณ์

เรียนรู้วิธีต่าง ๆ ในการจัดเก็บและจัดการข้อมูลภายในอุปกรณ์มือถือ รวมถึง SQLite, AsyncStorage และค่ากำหนดที่ใช้ร่วมกัน

การเก็บข้อมูลภายในอุปกรณ์ เป็นบทเรียน Indie Hacker Mobile Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Indie Hacker Mobile Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Indie Hacker Mobile Apps มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Store Data Locally?

When building mobile apps, some data needs to be available even when there's no internet connection. This is where local data persistence comes in!

It means storing information directly on the user's device, rather than always fetching it from a server.

Benefits of Local Storage

Storing data locally offers several advantages for your app:

  • Offline Access: Users can still use core features without internet.
  • Faster Performance: Retrieving data from the device is quicker than from a remote server.
  • Improved UX: A smoother, more responsive experience for your users.
  • Personalization: Saving user preferences and settings.

Simple Key-Value Pairs

One of the easiest ways to store small amounts of data is using key-value pairs. Think of it like a dictionary or a map where each piece of data has a unique name (the 'key') and its corresponding value.

This is perfect for user settings, feature flags, or simple flags like 'has seen onboarding'.

Shared Preferences (Android/iOS)

On native platforms, Android uses Shared Preferences and iOS uses UserDefaults for key-value storage. They work similarly:

  • Store simple data types (strings, numbers, booleans).
  • Are often synchronous.
  • Best for small amounts of non-sensitive data.

For cross-platform frameworks, you'll use an abstraction layer.

AsyncStorage: Cross-Platform K-V

For apps built with frameworks like React Native, AsyncStorage is a popular module for local key-value storage. It's an asynchronous, unencrypted, persistent key-value storage system.

Because it's asynchronous, your app won't freeze while data is being read or written.

Saving Data with AsyncStorage

Here's how you might conceptually save a user's theme preference using AsyncStorage. In a real app, AsyncStorage would be imported from a library.

// (Imagine AsyncStorage is globally available)

async function main() {
  console.log("Attempting to save user theme...");
  try {
    // In a real app, this would save to device storage.
    // await AsyncStorage.setItem('userTheme', 'dark');
    console.log("User theme 'dark' conceptually saved.");
  } catch (error) {
    console.log("Error saving data:", error.message);
  }
}

main();

Reading Data with AsyncStorage

To retrieve the saved data, you use the getItem method. It also returns a Promise, so you'll typically use await or .then().

// (Imagine AsyncStorage is globally available)

async function main() {
  console.log("Attempting to read user theme...");
  try {
    // In a real app, this would read from device storage.
    // const theme = await AsyncStorage.getItem('userTheme');
    const theme = "dark"; // Simulate a retrieved value
    console.log("Retrieved user theme:", theme);
  } catch (error) {
    console.log("Error reading data:", error.message);
  }
}

main();

SQLite: Relational Database

For more complex data, like lists of items, user profiles with multiple fields, or anything requiring structured queries, SQLite is a powerful option.

It's a lightweight, embedded relational database that runs directly on the device. Think of it as a mini-serverless database.

When to Use SQLite

SQLite is ideal when you need:

  • Structured Data: Tables, columns, and relationships.
  • Complex Queries: Filtering, sorting, joining data.
  • Large Datasets: More efficient than key-value for many items.
  • Offline Sync: Storing data that will eventually sync with a backend.

However, it adds more complexity to your app's architecture.

Quick Check: Data Storage

Which local data storage method is generally best suited for storing a user's preference for 'dark mode' in a cross-platform mobile app?

Recap: Local Data Persistence

You've learned about essential local data persistence techniques for mobile apps!

  • Key-Value Stores: Simple for small data (Shared Preferences/UserDefaults, AsyncStorage).
  • AsyncStorage: Cross-platform (React Native) key-value, asynchronous.
  • SQLite: Embedded relational database for structured, complex data.

Choosing the right method depends on your data's complexity and your app's specific needs. Next, we'll explore integrating with remote APIs!

คำถามที่พบบ่อย

บทเรียน “การเก็บข้อมูลภายในอุปกรณ์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเก็บข้อมูลภายในอุปกรณ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Indie Hacker Mobile Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Indie Hacker Mobile Apps มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเก็บข้อมูลภายในอุปกรณ์”

เรียนรู้วิธีต่าง ๆ ในการจัดเก็บและจัดการข้อมูลภายในอุปกรณ์มือถือ รวมถึง SQLite, AsyncStorage และค่ากำหนดที่ใช้ร่วมกัน คุณปฏิบัติ Indie Hacker Mobile Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Indie Hacker Mobile Apps หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Indie Hacker Mobile Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การเก็บข้อมูลภายในอุปกรณ์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Indie Hacker Mobile Apps นี้ได้ไหม

ได้ บทเรียน Indie Hacker Mobile Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การจัดการสถานะสำหรับมือถือ
  2. การเก็บข้อมูลภายในอุปกรณ์
  3. การเชื่อมต่อ RESTful API
  4. สถาปัตยกรรมการนำทางและการกำหนดเส้นทาง
← กลับไปที่ Indie Hacker Mobile Apps