All Products
Search
Document Center

APDataCenter

Last Updated: May 18, 2021

APDataCenter is a unified storage entry class. It is singleton and can be called anywhere in the code.

 
  1. [APDataCenter defaultDataCenter]

You can also use macros.

 
  1. #define APDefaultDataCenter [APDataCenter defaultDataCenter]

This initializes APDataCenter.

API description

Macro definitions

 
  1. #define APDefaultDataCenter [APDataCenter defaultDataCenter]
  2. #define APCommonPreferences [APDefaultDataCenter commonPreferences]
  3. #define APUserPreferences [APDefaultDataCenter userPreferences]
  4. #define APCurrentVersionStorage [APDefaultDataCenter currentVersionStorage]

Constants

These event notifications usually require no attention from the business-level codes, but Datacenter will throw these notifications.

 
  1. /**
  2. * Event notification on that the database file of the previous user is about to be closed.
  3. */
  4. extern NSString* const kAPDataCenterWillLastUserResign;
  5. /**
  6. * Notification on that the user status has been switched. It is possible that the user has changed to nil. The specific userId can be acquired with the currentUserId function.
  7. * The auxiliary object to this notification is a dictionary. If it is not nil, the @"switched" key value in it returns `@YES and it indicates that user switch event did happen.
  8. */
  9. extern NSString* const kAPDataCenterDidUserUpdated;
  10. /**
  11. * The user didn't switch, and `APDataCenter` receives the sign-in event again. This notification will be thrown.
  12. */
  13. extern NSString* const kAPDataCenterDidUserRenew;

APIs and properties

void APDataCenterLogSwitch(BOOL on);

Enable or disable the console log of Datacenter. It is enabled by default.

@property (atomic, strong, readonly) NSString* currentUserId;

The userId of the user currently logged in.

(NSString*)preferencesRootPath;

Get the path where the commonPreferences and userPreferences database folders are stored.

(void)setCurrentUserId:(NSString*)currentUserId;

Set the user ID of the user currently logged in. Do not call it in the business-level codes as it will be called by the login module. Once the user ID is set, userPreferences will point to the database of this user.

(void)reset;

Fully reset the Data Center directories. Please use it with caution.

(APSharedPreferences*)commonPreferences;

The user independent global storage database.

(APSharedPreferences*)userPreferences;

The storage database that the user currently logged in. When the user is not logged in, nil will be returned.

(APSharedPreferences)preferencesForUser:(NSString)userId;

Return the storage object of the specified user ID. The business layer usually uses userPreferences method for this purpose. When there is a need for asynchronous storage, this method can be used to acquire the storage database of a specified user to avoid data mess-up.

(APPreferencesAccessor)accessorForBusiness:(NSString)business;

Generate an data accessor based on the business name. The business layer needs to hold the object by itself. Once this data accessor is used, the business value will be no longer required for accessing the KV storage.

 
  1. APPreferencesAccessor* accessor = [[APDataCenter defaultDataCenter] accessorForBusiness:@"aBiz"];
  2. [[accessor commonPreferences] doubleForKey:@"aKey"];
  3. // Equal to
  4. [[[APDataCenter defaultDataCenter] commonPreferences] doubleForKey:@"aKey" business:@"aBiz"];

(APCustomStorage*)currentVersionStorage;

Datacenter will maintain a database of the current version. When the version is upgraded, the database will be reset.

(id<APDAOProtocol>)daoWithPath:(NSString*)filePath userDependent:(BOOL)userDependent;

Generate a DAO access object from a configuration file.

Parameter description

Parameter Description
filePath The path of the DAO configuration file. For files in the main bundle, use the method below:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"xml"];
userDependent Specifies the database operated by the DAO object.
If userDependent=NO, it indicates that it is not user-specific, and DAO object will create tables in the database files of commonPreferences.
If userDependent=YES, DAO object will create tables in the database files of userPreferences.
After the user is switched, the subsequent DAO operations will automatically switch to the files of the new user, and the business layer does not need to care about the user switch.

Return value

The DAO object. The business layer does not need to care about its class name but only needs to enforce the switch by using the custom id<AProtocol>. The DAO object returned can be switched with id<APDAOProtocol> as necessary. The method provided by default will be called. So the custom AProtocol should not contain methods defined in APDAOProtocol.

(id<APDAOProtocol>)daoWithPath:(NSString)filePath databasePath:(NSString)databasePath;

Create a DAO access object maintaining its own independent database files without using APSharedPreferences. The DAO object created using daoWithPath:userDependent: interface operates on commonPreferences or userPreferences. This interface will create a DAO object operating on the database file specified in the databasePath. If the file does not exist, it will be created. Multiple DAO objects can be created pointing to the same databasePath.

Parameter description

Parameter Description
filePath The same as daoWithPath:userDependent: interface.
databasePath The location of the DAO database file. The path can be an absolute path or a relative path, such as Documents/XXXX.db or Library/Movie/XXX.db.

Return value

DAO object.