Contentstack iOS Persistence Library
Contentstack 提供了 iOS Persistence Library,该库允许您的应用程序在设备的本地存储中存储数据。这有助于您构建即使离线也能工作的应用程序。以下是我们 iOS Persistence Library 的详细指南和实用资源,以帮助您开始。
先决条件
- 最新版的Xcode和Mac OS X
设置和初始化库
您可以与CoreData和Realm数据库一起使用iOS Persistence Library。让我们了解如何为您的项目设置这些。
针对CoreData
要为CoreData设置此库,请按照以下步骤进行。
使用 CocoaPods 安装
- 请将以下行添加到您的 Podfile 中:
pod 'ContentstackPersistenceCoreData'
- 执行 pod install,您应该现在拥有库的最新版本。
导入 header/module
#import <ContentstackPersistenceCoreData/ContentstackPersistenceCoreData.h>;
您还可以将其作为模块导入
- Objective-C
@import ContentstackPersistenceCoreData
- Swift
import ContentstackPersistenceCoreData
初始化库
要开始在您的应用程序中使用此库,您需要通过提供您的堆栈详细信息来初始化它。
- Objective-C
Config *config = [[Config alloc] init]; config.host = @"customcontentstack.io"; Stack *stack = [Contentstack stackWithAPIKey:<APIKey> accessToken:<AccessToken> environmentName:<EnvironmentName> config:config]; CoreDataStore *coreDataStore = [[CoreDataStore alloc] initWithContenxt: <NSManageObjectContext>]; SyncManager *syncManager = [[SyncManager alloc] initWithStack:stack persistance:coreDataStore] [syncManager sync:{ (percentageComplete, isSyncCompleted, error) in }];
- Swift
let config = Config() config.host = @"customcontentstack.io"; let stack : Stack = Contentstack.stack(withAPIKey: <APIKey>, accessToken: <AccessToken>, environmentName: <EnvironmentName>, config:config) var coreDataStore = CoreDataStore(contenxt: <NSManageObjectContext>) var syncManager : SyncManager = SyncManager(stack: stack, persistance: coreDataStore) syncManager.sync({ (percentage, isSynccompleted, error) in })
针对 Realm
为设置此库针对 Realm,请按照以下步骤操作:
使用 CocoaPods 安装
- 请将以下行添加到您的 Podfile 中:
pod 'ContentstackPersistenceRealm'
- 执行 pod install,您应该现在拥有库的最新版本。
导入 header/module
在 Objective-C 项目中,您可以按照以下方式导入头文件:
#import <ContentstackPersistenceRealm/ContentstackPersistenceRealm.h>;
您还可以将其作为模块导入
- Objective-C
@import ContentstackPersistenceRealm
- Swift
import ContentstackPersistenceRealm
初始化库
为了在您的应用程序中使用库,您需要通过提供堆栈详细信息来初始化它
- Objective- C
Config *config = [[Config alloc] init]; config.host = @"customcontentstack.io"; Stack *stack = [Contentstack stackWithAPIKey:<APIKey> accessToken:<AccessToken> environmentName:<EnvironmentName> config:config]; RealmStore *realmStore = [[RealmStore alloc] initWithRealm:[[RLMRealm alloc] init]]; SyncManager *syncManager = [[SyncManager alloc] initWithStack:stack persistance:realmStore] [syncManager sync:{ (percentageComplete, isSyncCompleted, error) in }];
- Swift
let config = Config() config.host = @"customcontentstack.io"; let stack : Stack = Contentstack.stack(withAPIKey: <APIKey>, accessToken: <AccessToken>, environmentName: <EnvironmentName>, config:config) var realmStore = RealmStore(realm: RLMRealm()) var syncManager : SyncManager = SyncManager(stack: stack, persistance: realmStore) syncManager.sync({ (percentage, isSynccompleted, error) in })
我们使用iOS Persistence Library创建了一个示例应用程序,该应用程序在设备本地存储中存储数据。《阅读教程》以开始使用示例应用程序。