APDataCenter is the singleton entry class for unified storage. It can be called from anywhere in the code.
[APDataCenter defaultDataCenter]You can also use a macro.
#define APDefaultDataCenter [APDataCenter defaultDataCenter]API introduction
Macros
#define APDefaultDataCenter [APDataCenter defaultDataCenter]
#define APCommonPreferences [APDefaultDataCenter commonPreferences]
#define APUserPreferences [APDefaultDataCenter userPreferences]
#define APCurrentVersionStorage [APDefaultDataCenter currentVersionStorage]Constants
Unified storage issues the following notifications. You do not typically need to handle these notifications in your application code.
/**
* Notification for when the previous user's database file is about to be closed.
*/
extern NSString* const kAPDataCenterWillLastUserResign;
/**
* Notification that the user state has changed. The user might have become nil.
* Get the specific user ID with currentUserId.
* The object attached to this notification is a dictionary. If the object is not nil,
* the @"switched" key returns @YES to indicate that a user switch event occurred.
*/
extern NSString* const kAPDataCenterDidUserUpdated;
/**
* Notification thrown when the user has not switched, but APDataCenter receives another logon event.
*/
extern NSString* const kAPDataCenterDidUserRenew;APIs and properties
void APDataCenterLogSwitch(BOOL on);
Enables or disables the unified storage console log. The log is enabled by default.
@property (atomic, strong, readonly) NSString* currentUserId;
Retrieves the user ID of the currently logged-on user.
(NSString*)preferencesRootPath;
Retrieves the path to the folder that stores the commonPreferences and userPreferences databases.
(void)setCurrentUserId:(NSString*)currentUserId;
Sets the ID of the currently logged-on user. Do not call this method from your application code. It must be called by the logon module. After you set the user ID, userPreferences points to that user's database.
(void)reset;
Completely resets the entire unified storage directory. Use this with caution.
(APSharedPreferences*)commonPreferences;
A global storage database that is not user-specific.
(APSharedPreferences*)userPreferences;
The storage database for the currently logged-on user. Returns nil if no user is logged on.
(APSharedPreferences*)preferencesForUser:(NSString*)userId;
Returns the storage object for a specified user ID. Although the application layer can typically use the userPreferences method, you should use this method to retrieve the storage database for a specific user. This prevents data corruption during asynchronous storage operations.
(APPreferencesAccessor*)accessorForBusiness:(NSString*)business;
Generates an accessor based on a business name. Your application must retain the returned object. After you use this accessor, you no longer need to pass the business name to access the key-value (KV) store.
APPreferencesAccessor* accessor = [[APDataCenter defaultDataCenter] accessorForBusiness:@"aBiz"];
[[accessor commonPreferences] doubleForKey:@"aKey"];
// Equivalent to
[[[APDataCenter defaultDataCenter] commonPreferences] doubleForKey:@"aKey" business:@"aBiz"];(APCustomStorage*)currentVersionStorage;
Unified storage maintains a database for the current application version. This database is reset when the application is upgraded.
(id<APDAOProtocol>)daoWithPath:(NSString*)filePath userDependent:(BOOL)userDependent;
Generates a Data Access Object (DAO) from a configuration file.
Parameters
Parameter | Description |
filePath | The file path of the DAO configuration file. For a file in the main bundle, use the following format: |
userDependent | Specifies which database the DAO operates on. If |
Return value
A DAO object. Your application does not need to be aware of its class name. You can simply cast it to your custom id<AProtocol>. The returned DAO object can also be cast to id<APDAOProtocol> to call the default methods. Therefore, your custom AProtocol must not declare any methods that are already defined in APDAOProtocol.
(id<APDAOProtocol>)daoWithPath:(NSString*)filePath databasePath:(NSString*)databasePath;
Creates a DAO that maintains its own database file instead of using APSharedPreferences. A DAO created with the daoWithPath:userDependent: method operates on commonPreferences or userPreferences. This method creates a DAO that operates on the specific database file specified by databasePath. If the specified file does not exist, this method creates it. You can create multiple DAO objects that specify the same databasePath.
Parameters
Parameter | Description |
filePath | Same as the |
databasePath | The location of the DAO database file. You can provide an absolute path or a relative path, such as |
Return value
A DAO object.