本文将带您了解关于Objective-c–最佳实践–iOS中的NSManagedObjectContextObjectsDidChangeNotification的新内容,另外,我们还将为您提供关于a
本文将带您了解关于Objective-c – 最佳实践 – iOS中的NSManagedObjectContextObjectsDidChangeNotification的新内容,另外,我们还将为您提供关于angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])、cocoa – 使用NSManagedObjectContext的objectWithID的“正确”方法是什么:、ios – NSManagedObject;保持或加载到自定义NSObject?、ios – NSManagedObjectContext已锁定的实用信息。
本文目录一览:- Objective-c – 最佳实践 – iOS中的NSManagedObjectContextObjectsDidChangeNotification
- angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])
- cocoa – 使用NSManagedObjectContext的objectWithID的“正确”方法是什么:
- ios – NSManagedObject;保持或加载到自定义NSObject?
- ios – NSManagedObjectContext已锁定
Objective-c – 最佳实践 – iOS中的NSManagedObjectContextObjectsDidChangeNotification
如何跟踪我的日历和地图视图中Notes和Location的更改?在viewDidLoad中加载它们很简单,但是如何跟踪所有更改,以便在用户重新访问地图视图时(例如),他/她也会看到最新的数据.
我解密的一种方法是在地图视图和日历视图中的NSManagedobjectContextObjectsDidChangeNotification中监听通知.这似乎从托管上下文返回所有插入,删除和更新的对象,每次都有保存.我可以通过这些对象,看看是否需要更新我的观点.这就是我在想这样做:
在MapViewController viewDidLoad中:
[[NSNotificationCenter defaultCenter] addobserver: self selector: @selector(objectChangednotificationReceived:) name: NSManagedobjectContextObjectsDidChangeNotification object: context];
然后:
- (void) objectChangednotificationReceived: (NSNotification *) notification{ NSArray* insertedobjects = [[notification userInfo] objectForKey:NSInsertedobjectsKey] ; NSArray* deletedobjects = [[notification userInfo] objectForKey:NSDeletedobjectsKey] ; NSArray* updatedobjects = [[notification userInfo] objectForKey:NSUpdatedobjectsKey] ; NSLog(@"insertObjects: %@",[insertedobjects description]); NSLog(@"deletedobjects: %@",[deletedobjects description]); NSLog(@"updatedobjects: %@",[updatedobjects description]); for (NSManagedobject *obj in insertedobjects) { if ([obj class] == [Location class]) { NSLog(@"adding a new location"); Location *locationObj = (Location *) obj; [self.mapview addAnnotation: locationObj]; } }}
这似乎是对的吗?似乎很多冗余代码放入每个视图控制器,特别是如果我对多个NSManagedobject感兴趣的话.还有其他一些我失踪的技术吗?
解决方法
angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])
Can’t resolve all parameters for AuthenticationService: ([object Object],?,[object Object])
我已经检查了几乎每个主题,并尝试了多种方法来解决它,但仍然无法在第二天击败它.
我试图像这样在appService中注入第一个authService但是得到了同样的错误
@Inject(forwardRef(() => AuthenticationService)) public authService: AuthenticationService
我检查了所有DI和服务内部的导入顺序,在我看来一切都是正确的
如果有人可以帮我处理它,我很感激.
Angular 4.0.0
AuthService
import { Injectable } from '@angular/core'; import {Http,Headers,Response} from '@angular/http'; import 'rxjs/add/operator/toPromise'; import {Observable} from 'rxjs/Rx'; import {AppServices} from "../../app.services"; import {Router} from "@angular/router"; @Injectable() export class AuthenticationService { public token: any; constructor( private http: Http,private appService: AppServices,private router: Router ) { this.token = localStorage.getItem('token'); } login(username: string,password: string): Observable<boolean> { let headers = new Headers(); let body = null; headers.append("Authorization",("Basic " + btoa(username + ':' + password))); return this.http.post(this.appService.api + '/login',body,{headers: headers}) .map((response: Response) => { let token = response.json() && response.json().token; if (token) { this.token = token; localStorage.setItem('Conform_token',token); return true; } else { return false; } }); } logout(): void { this.token = null; localStorage.removeItem('Conform_token'); this.router.navigate(['/login']); } }
应用服务
import {Injectable} from '@angular/core'; import {Headers,Http,RequestOptions} from '@angular/http'; import {Router} from "@angular/router"; import {AuthenticationService} from "./auth/auth.service"; import 'rxjs/add/operator/toPromise'; import {Observable} from 'rxjs/Rx'; @Injectable() export class AppServices { api = '//endpoint/'; public options: any; constructor( private http: Http,private router: Router,public authService: AuthenticationService // doesn't work // @Inject(forwardRef(() => AuthenticationService)) public authService: AuthenticationService // doesn't work either ) { let head = new Headers({ 'Authorization': 'Bearer ' + this.authService.token,"Content-Type": "application/json; charset=utf8" }); this.options = new RequestOptions({headers: head}); } // ==================== // data services // ==================== getData(): Promise<any> { return this.http .get(this.api + "/data",this.options) .toPromise() .then(response => response.json() as Array<Object>) .catch((err)=>{this.handleError(err);}) }
应用模块
import { browserModule } from '@angular/platform-browser'; import { browserAnimationsModule } from '@angular/platform-browser/animations'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import {BaseRequestOptions,HttpModule} from '@angular/http'; import { MaterialModule} from '@angular/material'; import {FlexLayoutModule} from "@angular/flex-layout"; import 'hammerjs'; import { routing,appRoutingProviders } from './app.routing'; import { AppServices } from './app.services'; import {AuthGuard} from "./auth/auth.guard"; import {AuthenticationService} from "./auth/auth.service"; import {AppComponent} from './app.component'; import {AuthComponent} from './auth/auth.component'; import {NotFoundComponent} from './404/not-found.component'; import { HomeComponent } from './home/home.component'; @NgModule({ declarations: [ AppComponent,AuthComponent,NotFoundComponent,HomeComponent ],imports: [ browserModule,browserAnimationsModule,FormsModule,HttpModule,routing,MaterialModule,FlexLayoutModule ],providers: [AppServices,AuthGuard,AuthenticationService],bootstrap: [AppComponent] }) export class AppModule { }
解决方法
你可以使用
export class AuthenticationService { public token: any; appService: AppServices; constructor( private http: Http,// private appService: AppServices,injector:Injector; private router: Router ) { setTimeout(() => this.appService = injector.get(AppServices)); this.token = localStorage.getItem('token'); }
另见DI with cyclic dependency with custom HTTP and ConfigService
要避免使用setTimeout,您还可以从AppService的构造函数中设置AuthenticationService.appService(或者相反)
cocoa – 使用NSManagedObjectContext的objectWithID的“正确”方法是什么:
…This method always returns an object.
The data in the persistent store
represented by objectID is assumed to
exist—if it does not,the returned
object throws an exception when you
access any property (that is,when the
fault is fired). The benefit of this
behavior is that it allows you to
create and use faults,then create the
underlying rows later or in a separate
context.
在Apple的’Core Recipes’示例应用程序中,该方法的结果用于填充NSFetchRequest,然后使用请求的结果,并对此结果进行注释:
// first get the object into the context Recipe *recipeFault = (Recipe *)[context objectWithID:objectID]; // this only creates a fault,which may NOT resolve to an object (for example,if the ID is for // an objec that has been deleted already): create a fetch request to get the object for real NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; [request setEntity: [NSEntityDescription entityForName:@"Recipe" inManagedobjectContext:context]]; nspredicate *predicate = [nspredicate predicateWithFormat: @"(self == %@)",recipeFault]; [request setPredicate: predicate];
我看过很多例子(其他代码和apple的’iClass’),其中objectWithID的结果直接使用 – 意思是,它的属性被访问并快速处理.
objectWithID是否应始终被视为“可能存在”的对象?
我问,因为我刚刚遇到这个并没有反对它的存在.
解决方法
您可以像对待它一样对待它,访问其属性等等,因为在后台Core Data将访问数据存储来为您的请求提供服务.但是,如果商店中不存在该对象,您将获得异常.
Here’s Apple’s documentation explaining faults(这是objectWithID:返回给你).
ios – NSManagedObject;保持或加载到自定义NSObject?
现在,我为Bob搜索Core Data.然后,我接受NSManagedobject Bob并将所有属性复制到NSObject Bob并使用它完成我需要的操作.
这是否有意义,还是应该在NSManagedobject中创建所需的方法? NSManagedobject可以像NSObject一样对待吗?
解决方法
参考:例如Paul Hegarty / Stanfords关于核心数据的iOS课程
ios – NSManagedObjectContext已锁定
>在主线程中,我更新实体中的键值或从Core Data获取一些行.
>在后台线程中,我从服务器下载数据.
但有时在[managedobjectContext executeFetchRequest上的核心数据更新/处理期间:请求错误:& error]
……我明白了:
semaphore_wait_signal_trap()中的#0 0x34507c5c
semaphore_wait_signal()中的#1 0x34507f58
#2 0x364d573a in pthread_mutex_lock () #3 0x35c91a2e in -[_PFLock lock] () #4 0x35c91a12 in -[NSPersistentStoreCoordinator lock] () #5 0x35c919e8 in -[NSManagedobjectContext(_NSInternalAdditions) lockObjectStore] () #6 0x35c90676 in -[NSManagedobjectContext executeFetchRequest:error:] ()
NSManagedobjectContext是如何锁定的?我能为此做些什么?
解决方法
最有可能的是,在单独的线程上管理上下文时遇到问题.确保为每个线程使用不同的上下文,并确保不在线程之间传递托管对象.在尝试访问另一个线程在一个线程上所做的更改之前,请确保合并上下文.
关于Objective-c – 最佳实践 – iOS中的NSManagedObjectContextObjectsDidChangeNotification的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])、cocoa – 使用NSManagedObjectContext的objectWithID的“正确”方法是什么:、ios – NSManagedObject;保持或加载到自定义NSObject?、ios – NSManagedObjectContext已锁定等相关内容,可以在本站寻找。
本文标签: