首页 \ 问答 \ 具有一对多关系的NSFetchedResultsController(NSFetchedResultsController with one-to-many relationship)

具有一对多关系的NSFetchedResultsController(NSFetchedResultsController with one-to-many relationship)

这是我无法理解使用Core Data的原因。 我想使用NSFetchedResultsControllerDelegate,我到目前为止看到的常用代码很容易理解,但总是基于一个实体模型。 所以你想在你的表中显示所有“事件”,你在Event实体上做一个获取请求,然后你就可以了。

问题是,我的模型是:

City (one-to-one) Company (one-to-many) Employees

我的表需要向员工展示,但是获取必须以城市为基础,以便检索公司,然后是员工,对吧? 我完全迷失了,我只是不知道怎么做。

因为如果我获取City或Company并且我将Employees放在NSMutableSet中,那么我不会松开所有authomatic UITableViewController同步吗? 例如,如果我这样做,我将无法做类似的事情

- (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = 
        [[_fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

This is something i just can't figure out working with Core Data. I want to work with an NSFetchedResultsControllerDelegate and the usual code i've seen so far is easy to understand but always based on a one entity model. So you want to show in your table all the "events", you do a fetch request on the Event entity and there, you are set to go.

The thing is, my model is:

City (one-to-one) Company (one-to-many) Employees

My table would need to show the employees, but the fetch would have to be based on the City, in order to retrieve the Company and then the Employees, right? I'm completely lost at this, i just don't see how to do the fetch.

Because if i fetch City or Company and i put Employees in an NSMutableSet, don't i loose all the authomatic UITableViewController syncing? For instance, if i do it like this, i will be unable to do something like

- (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = 
        [[_fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

原文:https://stackoverflow.com/questions/5820865
更新时间:2022-05-19 19:05

最满意答案

依赖注入模式中工厂的目的是为另一个实例生成实例,而其他实例不需要知道如何生成它。

从本质上讲,“工厂”只是一个对象返回者:在调用时返回实例的东西。

这在更有能力的语言中更容易看到。 例如,在Python中,类是可调用的(没有new运算符),并且调用类会生成类的实例。 因此,如果类不需要参数,那么类可以是它们自己的工厂。 同样,任何返回实例的零参数函数都可以视为工厂。 这使得依赖注入非常清晰,并且没有样板。

在更严格的语言中(Java / C ++ / C#静态类型传统,或者类或函数不像PHP那样完全是第一类),你需要隐藏“模式”背后的依赖注入,因为缺少“设计模式”语言功能 。 在PHP 5.3+中,您可以使用闭包作为工厂,或者您可以使用Java / C#方式并为每个工厂定义FactoryInterface和新类。

例如,对于您的课程,您可以这样做:

class Aprime extends A
{
    public function setSomeProperty($somePropertyFactory)
    {
        $this->_somePropertyObject = $somePropertyFactory();
    }
}

在这个类中, setSomeProperty需要一个零参数可调用的“工厂”,你可以像这样产生:

$other_dep_factory = function(){ return new SomeOtherClass(); };

或者像这样:

class ClassFactory {
    function __construct($classname, $args=array()) {
        $this->class = new ReflectionClass($classname);
        $this->args = $args;
    }

    function __invoke() {
        return $this->class->newInstanceArgs($this->args);
    }
}

$other_dep_factory = new ClassFactory('SomeOtherClass');

在PHP 5.3之前,你需要像Java一样:

interface IObjectFactory {
    function getObject();
}

// this B-and-D interface is optional
// it has no body because PHP doesn't support
// type-hinting return values
interface ISomeOtherClassFactory {}


class SomeOtherClassFactory implements ISomeOtherClassFactory {
    function getObject() {
        return new SomeOtherClass();
    }
}

class Aprime extends A
{
    public function setSomeProperty(ISomeOtherClassFactory $somePropertyFactory)
    {
        $this->_somePropertyObject = $somePropertyFactory->getObject();
    }
}


$other_dep_factory = new SomeOtherClassFactory();
$myAprimeObject->setSomeProperty($other_dep_factory);

那么你什么时候使用工厂? 每当一个对象需要创建另一个对象时。 如果对象只需要使用另一个对象,只需传入一个实例。


The purpose of a factory in dependency injection patterns is to produce instances for another instance, without that other instance needing to know how to produce it.

At its core, a "factory" is just an object-returner: something that returns an instance when invoked.

This is easier to see in more capable languages. For example in Python classes are callable (there is no new operator), and invoking a class produces an instance of the class. So classes can be their own factories if the class requires no arguments. Likewise any zero-argument function that returns an instance can be considered a factory. This makes dependency injection very clear and free-of-boilerplate.

In more rigid languages (Java/C++/C# static-typed tradition, or where classes or functions are not completely first-class like in PHP), you need to obscure dependency injection behind a "pattern", because "design patterns" are missing language features. In PHP 5.3+ you can use a closure as a factory, or you can go the Java/C# way and define a FactoryInterface and a new class per factory.

For example, with your class, you could do this:

class Aprime extends A
{
    public function setSomeProperty($somePropertyFactory)
    {
        $this->_somePropertyObject = $somePropertyFactory();
    }
}

In this class, setSomeProperty requires a zero-argument callable "factory", which you could produce like this:

$other_dep_factory = function(){ return new SomeOtherClass(); };

Or like this:

class ClassFactory {
    function __construct($classname, $args=array()) {
        $this->class = new ReflectionClass($classname);
        $this->args = $args;
    }

    function __invoke() {
        return $this->class->newInstanceArgs($this->args);
    }
}

$other_dep_factory = new ClassFactory('SomeOtherClass');

Prior to PHP 5.3, you need to do it like Java would:

interface IObjectFactory {
    function getObject();
}

// this B-and-D interface is optional
// it has no body because PHP doesn't support
// type-hinting return values
interface ISomeOtherClassFactory {}


class SomeOtherClassFactory implements ISomeOtherClassFactory {
    function getObject() {
        return new SomeOtherClass();
    }
}

class Aprime extends A
{
    public function setSomeProperty(ISomeOtherClassFactory $somePropertyFactory)
    {
        $this->_somePropertyObject = $somePropertyFactory->getObject();
    }
}


$other_dep_factory = new SomeOtherClassFactory();
$myAprimeObject->setSomeProperty($other_dep_factory);

So when do you use a factory? Whenever an object needs to create another object. If the object just needs to use another object, just pass in an instance.

相关问答

更多
  • [关于拦截器的Rant +代码被删除,因为那不是你需要的:)] 我发现属性注入通常是要走的路,因为它避免了通常不太有趣的样板代码 ...并且我将构造函数值中的第一个ISomething分配给属性...... 我不知道如何在Autofac中使用它(我主要使用Castle.Windsor)但我推荐它作为一种低维护和避免构造函数膨胀的好方法 编辑 :显然,马克塞曼提出的问题提到拦截作为处理这些案件的有效方式,所以我会放回原来的咆哮+代码。 我不确定它与他所指的相符,但它可能会给你一些想法 我非常喜欢Castle- ...
  • 依赖注入模式中工厂的目的是为另一个实例生成实例,而其他实例不需要知道如何生成它。 从本质上讲,“工厂”只是一个对象返回者:在调用时返回实例的东西。 这在更有能力的语言中更容易看到。 例如,在Python中,类是可调用的(没有new运算符),并且调用类会生成类的实例。 因此,如果类不需要参数,那么类可以是它们自己的工厂。 同样,任何返回实例的零参数函数都可以视为工厂。 这使得依赖注入非常清晰,并且没有样板。 在更严格的语言中(Java / C ++ / C#静态类型传统,或者类或函数不像PHP那样完全是第一类 ...
  • 如果要验证测试中登录的内容,则需要依赖注入。 此外,记录器很少是单例,通常每个类都有一个记录器。 观看面向对象设计的演示文稿 ,您可以看到为什么单例是坏的。 单身人士的问题是,它们代表着一个难以预测的全球性状态,特别是在测试中。 请记住,一个对象可以是事实上的单例,但是仍然可以通过依赖注入获得,而不是通过Singleton.getInstance() 。 我刚刚列出了Misko Hevery在演讲中提出的重点。 看完之后,您将获得充分的观点,了解为什么更好地让对象定义其依赖关系,但不能定义一种如何创建它们。 ...
  • 使用Factory(或任何其他Creation模式),调用者必须知道如何获取对象,并且必须在使用之前“明确地”请求它。 Car car = CarFactory.getCarByModel(LUXURY); 然而,当使用DI时,传递所需对象的责任被委托给一些外部(主要是容器)实体,该实体知道如何创建对象(通过读取已定义的配置)并使其可以静默地对调用者使用。 Car car = getCar(); void setCar(Car car){..} // container sets the car from ...
  • 您的方法中存在一些错误: 您的服务依赖于具体的LoggerFactory类型,这是一种依赖注入原则违规。 执行此额外初始化可能会使构建对象图不可靠,而注入构造函数应该很简单 。 它隐藏了ILogger是您的消费者所依赖的真实服务这一事实。 这使得系统更难以测试,更难以维护,并使对象图分析变得复杂。 使用工厂是一种气味,因为工厂几乎不是正确的解决方案 。 相反,您的服务应如下所示: public class MyService { private ILogger _logger; public ...
  • 你是对的。 您可以创建一个服务( AnimalFactory )来创建您想要使用的对象( DogAnimal , CatAnimal ,...)。 一个简单的例子可以是: class AnimalFactory { public function createAnimal($name) { // some logic here with $name $animal = new ...(); return $animal; } } $ ...
  • 开发的主要目标应该是使用最简单,最干净的代码来满足要求。 ( http://msdn.microsoft.com/en-us/magazine/cc163962.aspx ) 话虽如此,除非您有特定理由使用其中一种模式,否则请立即停止。 如果没有具体的理由去参与这项工作,那么你只需要编写更多的代码,希望它有一天会有用。 只要写出有效的方法。 如果需要,稍后重构它。 如果需要,继续沿着保持简单的道路前进。 选择一个模式,库或其他工具,使您可以编写干净,简单,可读的代码来完成工作。 The primary go ...
  • 在这里查看我的答案 https://stackoverflow.com/a/44793349/4335854 DI通过ServiceCollection完成,每个接口只能有一个实现,因此直接在服务集合上执行工厂模式可能不起作用,因为它不支持动态创建接口实现。 我在答案中所做的是创建了一个工厂的可注射实现,可以满足与可注射实现工厂相同的要求 Check out my answer here https://stackoverflow.com/a/44793349/4335854 DI is done thro ...
  • 如果你看看faker @ https://github.com/fzaninotto/Faker#localization的文档,你会发现你可以简单地将正确的本地化作为参数进行创建。 在您的情况下,只需使用: Faker\Factory::create('it_IT'); 定义工厂时,无需在匿名函数中添加更多参数。 编辑: 只是添加有关依赖注入的问题。 如果您跟踪源代码,它不会在下面执行任何依赖注入。 $factory->define(...) 仅设置一组定义 public function defin ...
  • 你可以简单地“手动”注入DAO: public class MyServlet extends HttpServlet { private MyService service = new MyService(new MyDao()); ... } 在你的测试中: public class MyServiceTest { private MyDao mockDao; private MyService service; @Before public vo ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。