ios_CoreData

CoreData基本概述:

CoreData:把model存在面向对象的数据库(可以存在本地),常用于存取数据以及复查查询等,它通用于ios&Mac os.

property list:包含六种常用类,分别为[NSArray,NSDictionary,NSNumber,NSString,NSData],这六种类的Model可以存储在本地.

在Xcode中通过Add Entity 去添加:Attributes(属性),Relationships(关系:从属关系/一对一/一对多),Fetched properties(查询获取),然后用Entity去Create NSManagedObject subClass,即创建了对应Model.


基本介绍及Method:

一:NSManagedDocument

1.NSManagedDocument:继承于NSDocument,提供了用于管理存储的方法,用于访问CoreData.

NSManagedDocument对象初始化方法:

UIManagedDocument *aDocument = [[UIManagedDocument alloc] initWithFileURL:(NSURL *)url;

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[url path]];//右边:取指定路径文件,然后用(BOOL  fileExists)判断文件是否存在.

if (fileExists) {

[aDocument openWithCompletionHandler:^(BOOL success) {

if (success) [self documentIsReady];
if (!success) NSLog(@"couldn't open document at %@",url);

}];

} else {

//在上面路径下去添加一个文件;

[self saveToURL:(NSString *)url 

forSaveOperation:UIDocumentSaveForCreating 

completionHandler:^(BOOL success) {   

if (success) [self documentIsReady];

if (!success) NSLog(@"couldn't create document at %@ ",url);

                }];//回调:执行完上面后取执行Block;

- (void)documentIsReady

{

if(self.document.documentState == UIDocumentStateNormal) {

NSManagedObjectContext *context = self.document.managedObjectContext;

//start using Document;

}

}

注意:

documentStates:

UIDocumentStateClosed

UIDocumentStateSavingError

UIDocumentStateEditingDisabled

UiDocumentStateInConflict


2.NSManagedDocument会对创建的document(文件)自动保存,也可以手动,即下面的方法:

//该方法属于异步操作,在A线程上执行该方法时会自动创建一个B线程去执行,然后completionHandler回调到A线程去执行;

- (void)saveToURL:(NSString *)url 

    forSaveOperation:UIDocumentSaveForOverwriting 

completionHandler:^(BOOL success) {     };

关闭文件:

- (void)closeWithCompletionHandler:^(BOOL success) {   };

3.UIManagedDocument的多个Entity之间不共享context,可以同NSNotificationController机制取获取改变通知.


二 :Context

NSManagedObjectContext:继承于UIDocument,它相当于CoreData的中心枢纽,可以用它对数据库中的对象进行增/删/该/查.

1.增:用context往Document文件插入对象:

NSManagedObjectContext  *context = aDocument.managedObjectContext;

NSManagedObject *photo = [NSEntityDescription insertNewObjectForEntityForName:@"photo"

inManagedObjectContext:context];

photo.title = [flickrData objectForKey:FLICKR_PHOTO_TITLE];//设置photo的title属性;

怎么访问Document文件里的对象呢?如果Document里面存储的Entity对象是NSDictionary类型的,可以:

- (id) valueForKey:(NSString *)key;

- (void) setValue:(id)value  forKey:(NSString *)key;

或:

- (id)valueForKeyPath:(NSIndexPath *)path;

- (void)setValue:(id)value  forKeyPath:(NSIndexPath *)path;

2.删:删除对象

for example:

[aDocument.managedObjectContext  deleteObject:photo];//如果有strong指针指向被删除的对象时,要记得把对象置为nil;

在删除前想要更新数据或做其他处理,可以利用Category添加方法:

for example:

@implementation Photo(Deletion)

- (void)prepareForDeletion

{

//do something you want;

}

三:NSFetchRequest

NSFetchRequest(取所需的对象):
设置:
1.Entity(对应的实体);
2.NSSortDescription(对取回的数组对象进行排序);
3.NSPredicate(查询条件).

for example:

NSRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photo"];

request.fetchBathSize = 20;//一次取20个对象;

request.fetchLimit = 100;//最大取对象数量限制;

request.sortDescriptors = @[sortDescriptor];//排序方式;

request.predicate= ...     //查询条件(如果不指定查询条件,那边系统会无限制的获取对象).

//定义sortDescriptor:

NSSortDescription *sortDescriptor = [NSSortDescription sortDescriptorWithKey:@"title"

   ascending:YES

        selector:@slector(sort:)];//执行@slector(sort:)进行排序;

NSPredicate(查询条件)

for example:

NSString *serverName = @"flickr";

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"thumbnailURL contains %@",serverName];//把包含serverName的对象都取出来;

可以组合查询条件:NSCompoundPredicate

for example:

NSArray *array = @[predicate1,predicate2];

NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:array];//and表示两个查询条件都满足;


for example:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photographer"];

NSData *yesterday = [NSData dataWithTimeIntervalSinceNow:-24*60*60];//指定时间:24小时;

request.predicate = [NSPredicate predicateWithFormat:@"any photos.uploadData > %@",yesterday];

request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascend ascending:YES]];

NSManagedObjectContext *context = aDocument.managedObjectContext;
NSError *error;
NSArray *photographers = [context executeFetchRequest:request error:&error];//增/删/改/查 都是通过context来执行的;
注意:数组里面的对象并没有实际获取过来,当访问其属性时才算真正取过来;


### 安全区、工作区、接待区的信息安全控制措施 #### 安全区的安全控制措施 为了确保敏感数据和关键系统的安全性,在安全部署物理和技术上的多重防护机制至关重要。应当设立专门的访问权限制度,仅授权特定人员进该区域,并过生物识别技术或其他高级认证手段验证身份[^1]。 对于信息系统而言,部署防火墙、侵检测/防御系统(IDS/IPS),定期更补丁程序以防止已知漏洞被利用。此外,还应安装防病毒件和其他恶意代码防范工具来保护服务器免受潜在威胁的影响。 ```python def check_security_zone_access(user_id, biometric_data): authorized_users = get_authorized_user_list() if user_id in authorized_users and verify_biometrics(biometric_data): grant_access_to_security_zone() else: deny_access_and_log_attempt() def protect_servers(): install_firewall_rules() configure_ids_ips_systems() apply_latest_patches() deploy_antivirus_software() ``` #### 工作区的安全控制措施 在工作区内,重点在于教育员工遵循良好的信息安全习惯以及实施有效的终端设备管理和网络流量监测策略。制定清晰的行为准则文档并向全体员工传达其重要性和具体要求;同时开展针对性培训课程提高大家对最攻击手法的认识水平。 针对计算机及其外设采取加密存储介质、设置强密码登录方式等预防性举措可以减少因人为失误造成的数据泄露风险。另外还需加强对无线局域网(WLAN)信号强度覆盖范围内的管控力度,避免非法接点带来的安全隐患。 ```bash # 设置工作站安全配置脚本 sudo apt-get update && sudo apt-get upgrade -y sudo ufw enable sudo systemctl start sshd.service sudo systemctl enable sshd.service echo "Ensure all workstations have strong password policies enforced." ``` #### 接待区的安全控制措施 考虑到访客可能携带未经过严格审查便携式计算装置的情况,在此区域内需特别注意公共Wi-Fi热点建设和使用规则说明牌摆放位置的选择。提供独立于企业内部生产环境之外的一个隔离型互联网连接服务给来访者使用,这样既能满足对方上网需求又不会影响到核心业务运作效率。 安排专人负责监督登记表单填写情况并及时提醒忘记签退离场的人士完成相应手续。当发现可疑行为时立即启动应急预案知相关部门介调查处理直至事件妥善解决为止。 ```sql CREATE TABLE visitor_logs ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), company VARCHAR(255), arrival_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, departure_time TIMESTAMP NULL ); INSERT INTO visitor_logs (name, company) VALUES ('John Doe', 'Example Corp'); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值