1

我正在构建一个与数据库通信的网络应用程序。我希望网络应用程序从我的数据库中下载所有信息,然后将其保存在本地,以便可以在本地访问和编辑(即使用户离线),然后同步回我的数据库(当用户在线时)。

我的数据库中的每一行都包含 2 个字符串和 3 个整数,我的数据库中有大约 1000-1500 行(这可能会让您了解我的数据库的大小)。

那么最好的方法是什么?是否可以以某种方式在用户设备上缓存此类数据?就像通过 JSON 检索数据库,而不是将其本地存储在用户可以访问和工作的 javascript 数组中(如前所述:即使用户离线,后面的部分也应该能够工作)。非常感谢您对此的所有意见。我至少在这里思考正确的方向吗?

编辑:我只想澄清我正在使用网络应用程序。使用 HTML、CSS 和 Javascript 开发的应用程序。我不是在做一个原生的目标 C iOS 应用程序。

4

4 回答 4

1
make a class of NSObject

@interface abc : NSObject<NSCoding>

@property..... // add properties that you want to save 

-(void)initWithDictionary:(NSMutableDictionary *)dictionary; // override init by passing dictionary with values of your properties

@end

in abc.m file 

implement these functions

-(void)initWithDictionary:(NSMutableDictionary *)dictionary
{

    yourProperty=[dictionary valueForKey:@"propertyKey"];

}

-(void) encodeWithCoder:(NSCoder *)aCoder{

    [aCoder encodeObject:yourProperty forKey:@"propertyKey"];

}

-(id) initWithCoder:(NSCoder *)aDecoder{

    self =[super init];

    if(self){

        [self setyourProperty:[aDecoder decodeObjectForKey:@"yourProperty"]];

    }
    return self;
}


now make a shared class that can be accessed from anywhere

@interface xyz : NSObject
{
    NSMutableArray *allItems;
}

+(xyz *) sharedStore;

-(NSMutableArray *)allItems;

-(abc *) createItem:(NSMutableDictionary *)dictionary;

-(NSString *) saveItemPath;

-(BOOL)saveChanges;


now in xyz.m implement these functions

-(id)init{

    self=[super init];

    if(self){

        NSString *path=[self saveItemPath];

        allItems=[NSKeyedUnarchiver unarchiveObjectWithFile:path];

        if(!allItems){

            allItems=[[NSMutableArray alloc]init] ;//]WithObjects:@"No more item in store", nil];

        }

    }

    return self;
}
+(xyz *)sharedStore{

    static xyz *sharedStore=nil;

    if(!sharedStore){

        sharedStore=[[super allocWithZone:nil]init];

    }

    return sharedStore;

}
+(id)allocWithZone:(NSZone *)zone{

    return [self sharedStore];

}
-(NSArray *)allItems{

    //[allItems insertObject:@"No more items are in store" atIndex:[allItems count]];
    return allItems;

}

-(abc *) createItem:(NSMutableDictionary *)dictionary
{
    abc *p=[[abc alloc] init];
    [p initWithDictionary:dictionary];
    [allItems insertObject:p atIndex:0];
    return p;
}

-(NSString *) saveItemPath
{
    NSArray *documentdirectories=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString * documentDirectory=[documentdirectories objectAtIndex:0];

    return [documentDirectory stringByAppendingPathComponent:@"items.archive"];

}

-(BOOL) saveChanges
{

    NSString *path=[self saveItemPath];

    return [NSKeyedArchiver archiveRootObject:allItems toFile:path];

}


now you can use the global variable like this

[xyz sharedStore]allItems]

now do one more thing, add this line to application did enter background

[xyz sharedStore]saveChanges];
于 2013-07-12T05:15:43.577 回答
0

您应该使用Core Data(它使用 SQLite)。

于 2012-04-03T22:13:49.143 回答
0

你需要研究持久性,最好的选择是 Core Data。例如,除了持久性之外,您还可以获得我和其他人在此处此处概述的许多其他巨大好处。如前所述,您可以使用 SQLite 作为应用程序的后备存储,然后您可以根据需要访问条目的对象表示。如果您正在处理同步,您可能还想查看 iCloud,您可以在此处找到有关信息。

于 2012-04-03T22:23:19.247 回答
0

如果您使用 Web 技术开发移动应用程序,存储数据的最佳方式是使用 SQLite。您可以离线或在线模式存储、检索数据。另一个优势是,如果您决定采用原生方式,您可以将数据库移植到任何移动设备或桌面应用程序。

如果您有更多问题或需要帮助,请联系我,我可以为您提供有关如何将 SQLite 与 Web 技术一起使用的更多信息?

如果您想节省为您的移动应用程序设计 Web GUI 的时间,您可以使用 Mobile Web 框架,它可以加快开发时间并允许您访问设备上的一些本机 API。我推荐 Sencha:http ://www.sencha.com/products/touch/ (基于 Html5、JS、CSS3 的非常好的框架)

于 2012-04-04T02:15:46.293 回答